Bokeh vbar Hover tool
Question:
I have a multiple data bar graph and want to show the max value of each bar upon hover. I have already changed the code a couple of times, but never succeeded…
Here is the code :
”’
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Panel, Tabs
from bokeh.models.tools import HoverTool
modalites = ['0-510','511-1002', '1003-2167', '>2168']
valeur1 = list(tables.loc[0])
valeur2 = list(tables.loc[1])
valeur3 = list(tables_2.loc[0])
valeur4 = list(tables_2.loc[1])
source1 = ColumnDataSource({'x' : modalites, 'valeur 1' : valeur1, 'valeur 2' : valeur2})
source2 = ColumnDataSource({'x' : modalites, 'valeur 1' : valeur3, 'valeur 2' : valeur4})
p1 = figure(title ='Répartition des sinistres en fonction des surfaces', x_range = modalites, plot_width=600, plot_height=400)
p2 = figure(title ='Répartition des sinistres en fonction des surfaces', x_range = modalites, plot_width=600, plot_height=400)
from bokeh.transform import dodge
abscisses_1 = dodge(field_name = 'x',
value = -0.25,
range = p1.x_range)
abscisses_2 = dodge(field_name = 'x',
value = 0,
range = p1.x_range)
p1.vbar(x = abscisses_1, top = 'valeur 1', width = 0.2, source = source1, color = 'green', legend = 'pas de sinistre')
p1.vbar(x = abscisses_2, top = 'valeur 2', width = 0.2, source = source1, color = "red", legend = "sinistre")
p1.legend.location = "top_left"
p1.legend.orientation = "horizontal"
p1.xgrid.grid_line_color = None
p2.vbar(x = abscisses_1, top = 'valeur 1', width = 0.2, source = source2, color = 'green', legend = 'pas de sinistre')
p2.vbar(x = abscisses_2, top = 'valeur 2', width = 0.2, source = source2, color = "red", legend = "sinistre")
p2.legend.location = "top_right"
p2.legend.orientation = "horizontal"
p2.xgrid.grid_line_color = None
tab1=Panel(child=p1, title='en %')
tab2=Panel(child=p2, title='en valeur absolue')
tabs=Tabs(tabs=[tab1, tab2])
h2 = HoverTool(tooltips = [( "nb bâtiments concernés:", "@top")])
p2.add_tools(h2)
show(tabs)
”’
If it helps, here is list(tables_2.loc[0]):[2303, 2184, 1909, 1511]
and list(tables_2.loc[1]):[271, 418, 587, 1046]
Answers:
The problem of your code is in the line where you define your HoverTool. You are using @top
, but in your ColumnDataSource top
is not defiened.
h2 = HoverTool(tooltips = [( "nb bâtiments concernés:", "@top")])
If your want to use top as the maximum of valeur1
and valeur2
you may have to write something like this.
top = [max(v1,v2) for v1, v2 in zip(valeur3, valeur4)]
source2 = ColumnDataSource({'x' : modalites, 'valeur1' : valeur3, 'valeur2' : valeur4, 'top': top})
In this case top
is well defiened and your code should work without changes.
By the way. You have defiened a HoverTool only for p2
. If you need one on p1
as well, you have to do similar things on source1
.
I have a multiple data bar graph and want to show the max value of each bar upon hover. I have already changed the code a couple of times, but never succeeded…
Here is the code :
”’
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Panel, Tabs
from bokeh.models.tools import HoverTool
modalites = ['0-510','511-1002', '1003-2167', '>2168']
valeur1 = list(tables.loc[0])
valeur2 = list(tables.loc[1])
valeur3 = list(tables_2.loc[0])
valeur4 = list(tables_2.loc[1])
source1 = ColumnDataSource({'x' : modalites, 'valeur 1' : valeur1, 'valeur 2' : valeur2})
source2 = ColumnDataSource({'x' : modalites, 'valeur 1' : valeur3, 'valeur 2' : valeur4})
p1 = figure(title ='Répartition des sinistres en fonction des surfaces', x_range = modalites, plot_width=600, plot_height=400)
p2 = figure(title ='Répartition des sinistres en fonction des surfaces', x_range = modalites, plot_width=600, plot_height=400)
from bokeh.transform import dodge
abscisses_1 = dodge(field_name = 'x',
value = -0.25,
range = p1.x_range)
abscisses_2 = dodge(field_name = 'x',
value = 0,
range = p1.x_range)
p1.vbar(x = abscisses_1, top = 'valeur 1', width = 0.2, source = source1, color = 'green', legend = 'pas de sinistre')
p1.vbar(x = abscisses_2, top = 'valeur 2', width = 0.2, source = source1, color = "red", legend = "sinistre")
p1.legend.location = "top_left"
p1.legend.orientation = "horizontal"
p1.xgrid.grid_line_color = None
p2.vbar(x = abscisses_1, top = 'valeur 1', width = 0.2, source = source2, color = 'green', legend = 'pas de sinistre')
p2.vbar(x = abscisses_2, top = 'valeur 2', width = 0.2, source = source2, color = "red", legend = "sinistre")
p2.legend.location = "top_right"
p2.legend.orientation = "horizontal"
p2.xgrid.grid_line_color = None
tab1=Panel(child=p1, title='en %')
tab2=Panel(child=p2, title='en valeur absolue')
tabs=Tabs(tabs=[tab1, tab2])
h2 = HoverTool(tooltips = [( "nb bâtiments concernés:", "@top")])
p2.add_tools(h2)
show(tabs)
”’
If it helps, here is list(tables_2.loc[0]):[2303, 2184, 1909, 1511]
and list(tables_2.loc[1]):[271, 418, 587, 1046]
The problem of your code is in the line where you define your HoverTool. You are using @top
, but in your ColumnDataSource top
is not defiened.
h2 = HoverTool(tooltips = [( "nb bâtiments concernés:", "@top")])
If your want to use top as the maximum of valeur1
and valeur2
you may have to write something like this.
top = [max(v1,v2) for v1, v2 in zip(valeur3, valeur4)]
source2 = ColumnDataSource({'x' : modalites, 'valeur1' : valeur3, 'valeur2' : valeur4, 'top': top})
In this case top
is well defiened and your code should work without changes.
By the way. You have defiened a HoverTool only for p2
. If you need one on p1
as well, you have to do similar things on source1
.