0

I am trying to build a bar chart where I have three legends (so three different bar charts) combined with a hoverTool.

At the moment I have build the bar charts with the legend, however I am having problems with the HoverTool. In the code showed below the hover tool shows all three tooltips for all of the charts. I only want the hovertool to show one of the tooltips, so for example for the bar chart 'popPerc' I only want to see '@population' in the hover tool.

p = figure(x_range=df2.district,plot_height=300,plot_width=500,
           y_range= ranges.Range1d(start=0,end=25))

p.xgrid.grid_line_color=None
p.ygrid.grid_line_color=None
p.y_range.start = 0
p.xaxis.major_label_orientation = 0.5
p.yaxis.visible = False
p.toolbar_location=None
p.outline_line_color = None
colors = all_palettes['BuGn'][3]
bar = {}
items = []
color = 0

features = ['popPerc','areaPerc','treesPerc']

for indx,i in enumerate(features):
    bar[i] = p.vbar(x='district', top=i, source=df2, muted_alpha=0, muted=False,
                   width= 0.8, color=colors[color])
    items.append((i,[bar[i]]))
    color+=1

legend = Legend(items=items,location=(0,100))
p.add_tools(HoverTool(tooltips = [('Trees','@trees'),
                                  ('Population','@population'),
                                  ('Area [km^2]','@area')]))
p.add_layout(legend,'left')
p.legend.click_policy='hide'
show(p)

Hope that someone can help, thanks in advance! :)

andKaae
  • 173
  • 1
  • 13
  • 1
    Can you add an example picture or - even better - include some example data, I find it difficult to visualize how exactly your figure would look like. I tried to just take some nonsense data, but these resulted in "weird" graphs (labeling didn't really make sense). I think you might be looking for [this SO question](https://stackoverflow.com/questions/27545842/hovertool-for-multiple-data-series-in-bokeh-scatter-plot#34534995). – syntonym Apr 28 '20 at 12:45
  • Of course, sorry for not doing that to begin with! Here is a snippet of the dataframe which should work with the code also: `df2 = pd.DataFrame(data={'district': ['Indre By','Vesterbro','Valby'], 'popPerc': [8.9,11.6,9.6], 'areaPerc': [10,9.1,10.3], 'treesPerc': [10.7,11.8,8.6], 'trees': [6105,6710,4878], 'area': [9,8.2,9.2], 'population': [55866,72688,60308]})` – andKaae Apr 30 '20 at 06:27

1 Answers1

1

After reading up on the article sugested I figured it out. By changing the code block for the hovertool to the following it works.

p.add_tools(HoverTool(renderers=[items[0][1][0]], tooltips = [('Population','@population')]))
p.add_tools(HoverTool(renderers=[items[1][1][0]], tooltips = [('Area [km^2]','@area')]))
p.add_tools(HoverTool(renderers=[items[2][1][0]], tooltips = [('Trees','@trees @treePerc')]))
andKaae
  • 173
  • 1
  • 13