If you don’t want to click the link here is the solution I was trying to adapt to violin/stacked bar charts
"""
Tested on bokeh 0.11.1, pandas 0.18.0
First plot has no groups and correct hover data,
Second plot has groups but misaligned hover data.
"""
from bokeh.sampledata.autompg import autompg as df
from bokeh.charts import Scatter, TimeSeries, output_notebook, show
from bokeh.models import HoverTool, BoxZoomTool, ResetTool, CrosshairTool, BoxSelectTool, WheelZoomTool, PreviewSaveTool
from bokeh.models.renderers import GlyphRenderer
import copy
hover = HoverTool(
tooltips = [
("cyl", "@cyl"),
("displ", "@displ"),
("Weight", "@weight"),
("Acceleration", "@accel"),
('Horsepower', '@hp'),
('MPG', '@mpg'),
('Origin', '@origin'),
])
tools = [hover, BoxZoomTool(), ResetTool(), CrosshairTool(), BoxSelectTool(), WheelZoomTool(), PreviewSaveTool()]
scatter_sans_groups = Scatter(df, x='mpg', y='hp', title="Auto MPG", xlabel="Miles Per Gallon", ylabel="Horsepower",
tools=tools)
``
def patch_renderer(scatter_instance, hover_instance):
tooltip_fields = [value[1:] for label, value in hover_instance.tooltips if value[0] == '@']
glyph_renderer = [r for r in scatter_instance.renderers if isinstance(r, GlyphRenderer)][0]
renderer_fields = glyph_renderer.data_source.data.keys()
fields_to_add = [tooltip_field for tooltip_field in tooltip_fields if tooltip_field not in renderer_fields]
print 'tooltip_fields: ', tooltip_fields
print 'renderer_fields: ', renderer_fields
print 'fields_to_add: ', fields_to_add
print 'counts before adding: ', [(key, len(glyph_renderer.data_source.data[key])) for key in glyph_renderer.data_source.data]
for field in fields_to_add:
glyph_renderer.data_source.data[field] = list(df[field])
print 'counts after adding: ', [(key, len(glyph_renderer.data_source.data[key])) for key in glyph_renderer.data_source.data]
patch_renderer(scatter_sans_groups, hover)
``
tooltip_fields: ['cyl', 'displ', 'weight', 'accel', 'hp', 'mpg', 'origin']
renderer_fields: ['x_values', 'chart_index', 'y_values']
fields_to_add: ['cyl', 'displ', 'weight', 'accel', 'hp', 'mpg', 'origin']
counts before adding: [('x_values', 392), ('chart_index', 392), ('y_values', 392)]
counts after adding: [('origin', 392), ('mpg', 392), ('chart_index', 392), ('displ', 392), ('weight', 392), ('hp', 392), ('accel', 392), ('x_values', 392), ('y_values', 392), ('cyl', 392)]
···
On Wednesday, August 24, 2016 at 4:02:00 PM UTC-4, Reuben Jacobs wrote:
Like the title says I have data I want to display in my hover for both violin and stacked bar charts. While I know bokeh doesn’t support showing data other than the x or y axis innately, I came across a solution for this problem for scatter charts. The solution can be found here
http://nbviewer.jupyter.org/github/billsanto/notebook_examples/blob/master/bokeh_hover_test.ipynb
I am trying to implement this solution but I fear it is not possibly for violin and stacked bar charts. Does anyone know how to display hover tools for data that is in the original dataFrame but not the x or y axis specifically for stacked bar or violin charts?
I tried implementing the solution found above, but I keeped receiving KeyErrors when trying to access the GlyphRenderer of my chart. Obviously for the stacked bar chart and violin chart there are 100+ GlyphRenderer which is why I believe this solution is not working.
Thanks.