简体   繁体   English

在堆积条形图中包含散景工具提示

[英]Include Bokeh Tooltips in stacked bar chart

I have the code below. 我有下面的代码。 Would anyone be able to let me know how to include tooltips for the bar chart below. 有人能告诉我如何在下面的条形图中包含工具提示。

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)

Thanks 谢谢

Michael 迈克尔

You can add a hovertool by specifying "hover" in the list with tools and adding tooltips to it. 您可以通过使用工具在列表中指定“悬停”并向其添加工具提示来添加hovertool。 You have two kinds of tooltips; 你有两种工具提示; "@" which displays sourcedata and $ which correspond to values that are intrinsic to the plot, such as the coordinates of the mouse in data or screen space. “@”显示sourcedata和$,它们对应于绘图固有的值,例如数据或屏幕空间中鼠标的坐标。 Hovertools are nice to use in combination with a ColumnDataSource so also take a look at that. Hovertools很适合与ColumnDataSource结合使用,所以也看看它。 More information on hovertools can be found here . 有关hovertools的更多信息,请点击此处

Adding a hovertool to your plot can be done by changing these lines: 可以通过更改以下行来为您的绘图添加hovertool:

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)

You want tooltips to indicate the value by year: 您希望工具提示按年指示值:

tooltips = [
    ("fruit", "@fruits"),
    ("2015:", "@2015"),
    ("2016:", "@2016"),
    ("2017:", "@2017"),
]

p = figure(x_range=fruits, plot_height=300, title="Fruit Counts by Year",
           tooltips=tooltips,
           toolbar_location="right", tools="")

output: 输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM