Reputation: 2472
So I have a bar chart with a line on top of it. It works fine except that I cannot get the hovertool to work on the added line graph. I am not sure how to complete the code below to get the hovering to work for the line. Any help would be appreciated.
from bokeh.plotting import figure, ColumnDataSource
from bokeh.embed import components
from bokeh.models import LinearAxis, Range1d, HoverTool
def convert_to_categorical(months):
month_names = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
return [month_names[m-1] for m in months]
def NetRevGraph(NetRev, Vol):
# bar chart (hovering works fine)
x1 = [int(i['Month']) for i in NetRev]
y1 = [i['Amount'] for i in NetRev]
m1 = convert_to_categorical(x1)
tooltip_net_rev = [("Net Revenue", "$y"),]
source_net_rev = ColumnDataSource(data=dict(x=m1, y=y1, ))
p = figure(plot_width=500, plot_height=400, x_range=m1, tooltips=tooltip_net_rev)
p.vbar(x='x', width=0.5, bottom=0, top='y', color="#B3DE69", source=source_net_rev)
# add line graph (can seem to complete this so that hover works)
x2 = [int(i['MonthNum']) for i in Vol]
y2 = [i['Val'] for i in Vol]
m2 = convert_to_categorical(x2)
source_vol = ColumnDataSource(data=dict(x=m2, y=y2, ))
tooltip_net_vol = [("Volume", "$y"), ]
p.extra_y_ranges = {"Vol": Range1d(start=min(y2), end=max(y2))}
p.add_tools()
p.line(x='x', y='y', line_width=2, y_range_name="Vol", color="black", source=source_vol)
p.add_layout(LinearAxis(y_range_name="Vol"), 'right')
Upvotes: 2
Views: 398
Reputation: 2472
Figured it out. I had to use the add_tools method for both the bar and the line to add HoverTool for both.
updated figure to take no hover arguments. Gave the bar chart a variable name and then used it to add the hover tool.
p = figure(plot_width=500, plot_height=400, x_range=m1)
bar = p.vbar(x='x', width=0.5, bottom=0, top='y', color="#B3DE69", source=source_net_rev)
p.add_tools(HoverTool(renderers=[bar], tooltips=tooltip_net_rev))
Did the same with the line graph
line = p.line(x='x', y='y', line_width=2, y_range_name="Vol", color="black", source=source_vol)
p.add_tools(HoverTool(renderers=[line], tooltips=tooltip_net_vol))
Upvotes: 2