Data tooltips in Bokeh don't show data, showing '???' instead

天大地大妈咪最大 提交于 2019-11-27 14:31:43

I was having the same problem. I found this reference useful. The tooltip for Sales would use the generic @height, e.g.: hover.tooltips = [('Sales', '@height')]

Similarly, replacing @height with @y would give you the total sales for each year. I haven't figured out how to use the tooltip to access the stacked categories or how to use the ColumnDataSource referred to in the link.

I was able to recreate your problem and found a solution. First, the recreation of your DF:

data = [k.split() for k in 
'''2016    A           1
2016    B           1
2016    C           1.5
2017    A           2
2017    B           3
2017    C           1
2018    A           2.5
2018    B           3
2018    C           2'''.split('\n')]

x = pd.DataFrame(data, columns = ['year','category','sales'])
x['year']  = x['year'].astype(object)
x['sales'] = x['sales'].astype(float)

Now the solution:

from bokeh.charts import Bar, output_file, show
from bokeh.models import HoverTool
from bokeh.models import ColumnDataSource    

source = ColumnDataSource(x)

bar = Bar(x, label='year', values='sales', agg='sum', stack='category', title="Expected Sales by year", tools = 'hover')
hover = bar.select(dict(type=HoverTool))
hover.tooltips = [('Year', '@x'),('Sales', '@y')]
show(bar)

Which produces the following chart:

The addition of:

class ColumnDataSource(*args, **kw)

is probably the most important part of the solution (you can read more about it here).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!