Hovertips in pos/neg stacked vbar

I want to create a stacked bar chart showing contributions to the total from different categories. I can do this by splitting the data into positive and negative data, and then plotting separate stacked bars from both. How can I then clean up the hover tools, so instead of that showing two sets, one for the positive CDS and one for negative CDS, there would just be one? So, instead of 2015 showing

Apples: 1
Nectarines: 0


Apples: 0
Nectarines: -4

It would just show:
Apples: 1
Nectarines: -4

d = {'Apples' : [1,-5,8],
     'Pears' : [3,-9,4],
     'Nectarines' : [-4, 7, -6]}

df = pd.DataFrame(d,index=['2015', '2016', '2017'])

df.rename_axis('Date', axis=0, inplace=True)
df = df.apply(pd.to_numeric)

pos = df.mask(df <0)
neg = df.mask(df >0)

p = figure()

cats = ['Apples','Pears','Nectarines' ]
labels = ['Apples','Pears','Nectarines']

colors = ['blue', 'steelblue', 'lightblue',]

p.vbar_stack(stackers=cats, x="Date", width=0.3, source=ColumnDataSource(m), color = colors, legend_label=labels)
p.vbar_stack(stackers=cats, x="Date", width=0.3, source=ColumnDataSource(x), color = colors, legend_label=labels)

hover = HoverTool()
hover.tooltips = [('Date', '@Date{%F}'),
                  ('Apples', '@{Apples}{0,0.0}'),
                  ('Pears', '@{Pears}{0,0.0}'),
                  ('Nectarines', '@{Nectarines}{0,0.0}')]
hover.formatters = {'@Date': 'datetime'}


Since you would need to coordinate multiple CDS, the only option I can think of would be to use a CustomJSHover for each field. You can pass in both CDS in the args dict so that you can access whichever you need for the given field. Of course, this all naturally assume that the CDS line up very similarly, so that the inspection indices from whatever glyph the mouse is actually over, are useful to index into the other CDS. If that’s not the case, then I think you would need to look for a different overall approach (e.g. “stacking” manually in order to be able to share a single CDS).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.