from datetime import datetime, timedelta
import pandas as pd
from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource, HoverTool, Range1d, DatetimeTickFormatter
fill_color = []
y= [ -331.6, -1114.8, -2299.9, -1509.2, 240.7, -43.2, -1623.2,
-1301.2, -1562.6, -209.4, -208.1, -735.4, 81.4, -208.8,
-187.7, -480.7, -141.4, 440.2, -855.7, -214.1, 1106.9,
-1257.4, -1722.7, -672.9, 163. , -471. , 17.1, -128.7,
-639.5, -863.1, -62.2, -299.9, -547.3, 36.3, 545.1,
116. , -46.5, -214.8, -116.6, 58.9, -454.9, 265.5,
816.5, 549.9, 64.3, 728.6, 1758.3, -1119.4, -898.7,
912.9]
for a in y:
if a >= 0:
fill_color.append('green')
elif a < 0:
fill_color.append('red')
dates = ['2024-11-05', '2024-11-06', '2024-11-07', '2024-11-08',
'2024-11-11', '2024-11-12', '2024-11-13', '2024-11-14',
'2024-11-15', '2024-11-18', '2024-11-19', '2024-11-20',
'2024-11-21', '2024-11-22', '2024-11-25', '2024-11-26',
'2024-11-27', '2024-11-28', '2024-11-29', '2024-12-02',
'2024-12-03', '2024-12-04', '2024-12-05', '2024-12-06',
'2024-12-09', '2024-12-10', '2024-12-11', '2024-12-12',
'2024-12-13', '2024-12-16', '2024-12-17', '2024-12-18',
'2024-12-19', '2024-12-20', '2024-12-23', '2024-12-24',
'2024-12-25', '2024-12-26', '2024-12-27', '2024-12-30',
'2024-12-31', '2025-01-02', '2025-01-03', '2025-01-06',
'2025-01-07', '2025-01-08', '2025-01-09', '2025-01-10',
'2025-01-13', '2025-01-14']
x = pd.to_datetime(dates)
p = figure(width=1500, height=400, x_axis_type='datetime')
p.xaxis.major_label_overrides = {
i: date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(dates))
}
p.xaxis.formatter = DatetimeTickFormatter(days=["%d %b %Y"])
p.vbar(x=x, width=45000000, bottom=0, top=y, color = fill_color)
p.add_tools(HoverTool(tooltips=[("date", "@x{%F}"),( "value", "@top{"f"0.0""}"),], formatters={'@x':'datetime',},))
p.x_range = Range1d(x[-31]+timedelta(days=0.3), x[-1]+timedelta(days=0.7), bounds=(x[0]-timedelta(days=0.7), x[49]+timedelta(days=0.7)))
p.y_range = Range1d(1.5 * min(y), 1.5 * max(y))
show(p)
I’ve referenced this post, but still fail to reproduce the same effect.