python - 在堆积条形图中包括散景工具提示
问题描述
我有下面的代码。谁能让我知道如何为下面的条形图添加工具提示。
from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.plotting import figure
output_file("stacked.html")
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
toolbar_location=None, tools="")
p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data,
legend=[value(x) for x in years])
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
show(p)
谢谢
迈克尔
解决方案
您可以通过在工具列表中指定“悬停”并向其添加工具提示来添加悬停工具。您有两种工具提示;"@" 显示 sourcedata 和 $,它们对应于绘图固有的值,例如鼠标在数据或屏幕空间中的坐标。Hovertools 很适合与ColumnDataSource结合使用,因此也可以查看一下。更多关于 hovertools 的信息可以在这里找到。
可以通过更改以下行来为您的绘图添加悬停工具:
tooltips = [
("fruit", "@fruits"),
("x, y", "$x,$y"),
]
p = figure(x_range=fruits, plot_height=300, title="Fruit Counts by Year",
toolbar_location="right", tools=["hover"], tooltips = tooltips)
推荐阅读
- c# - 使用上一个和下一个按钮在下拉列表中导航项目
- javascript - 将数据从搜索栏发送到 url
- c - 我正在用 C 进行快速排序,但看不出有什么问题
- typescript - 品牌字符串作为字典的键
- tensorflow - 相机倾斜时如何获得正确的边界框
- typescript - 使用 Jasmine 监视 TypeScript 中没有对象的非导入函数
- google-api - 控制 NEST 相机的 API?
- r - 将 hexbin 视为 data.frame 并在列中显示计数
- c++ - Chromium 浏览器:如何移动 BubbleDialogDelegateView 小部件而不闪烁?
- javascript - 在最后一行的最后一列上按 Tab 时如何插入新行