添加悬停以绘制多个垂直条散景

PER*_*Zje 2 python bokeh

因此,我基本上遵循了散景文档站点上有关处理分类数据的示例:

https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html

最终我得到了这段代码(我简化了一点):

# dictionary with data for making a figure
data = {'continents' : continents,
        '2016' : list2016,
        '2017' : list2017,
        '2018' : list2018 }


source = ColumnDataSource(data=data)

p = figure(x_range=continents, y_range=(0, 450), plot_height=250, title="University count per continent per year",
           toolbar_location=None, tools="")

p.vbar(x=dodge('continents', -0.25, range=p.x_range), top='2016', width=0.2, source=source,
       color="#c9d9d3", legend=value("2016"))

p.vbar(x=dodge('continents',  0.0,  range=p.x_range), top='2017', width=0.2, source=source,
       color="#718dbf", legend=value("2017"))

p.vbar(x=dodge('continents',  0.25, range=p.x_range), top='2018', width=0.2, source=source,
       color="#e84d60", legend=value("2018"))

p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_right"
p.legend.orientation = "horizontal"
Run Code Online (Sandbox Code Playgroud)

其中数据列有 4 个键,大洲是“类别”,2016、2017 和 2018 是大洲列表中每个大洲对应的值列表。例如:

continents = ["Africa", "Europe"]
list2016 = ["1", "3"] 
list2017 = ["4", "2"]
list2018 = ["14", "3"]
Run Code Online (Sandbox Code Playgroud)

所以这基本上是非洲在 2016 年、2017 年和 2018 年分别有 1、4 和 14 件东西。现在我想做的是在这些不同的垂直条上添加悬停,这样如果我悬停在某个条上,它就会给我那个年份/大陆的数字计数。这可能吗?

big*_*dot 6

因为0.13,您可以为每个字形指定一个name值,然后在工具提示中将其引用为$name. 此外,您可以使用@$name. 为方便起见,您也tooltips可以figure直接传递给。全部一起:

p = figure(x_range=continents, y_range=(0, 20), plot_height=250,
           title="University count per continent per year",
           toolbar_location=None, tools="hover", 
           tooltips="@continents $name: @$name")

p.vbar(x=dodge('continents', -0.25, range=p.x_range), top='2016', width=0.2, source=source,
       color="#c9d9d3", legend=value("2016"), name="2016")

p.vbar(x=dodge('continents',  0.0,  range=p.x_range), top='2017', width=0.2, source=source,
       color="#718dbf", legend=value("2017"), name="2017")

p.vbar(x=dodge('continents',  0.25, range=p.x_range), top='2018', width=0.2, source=source,
       color="#e84d60", legend=value("2018"), name="2018")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明