gpt4 book ai didi

python - 添加悬停以绘制具有多个垂直条的 Bokeh 图

转载 作者:行者123 更新时间:2023-12-01 22:43:28 25 4
gpt4 key购买 nike

因此,我基本上遵循了 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"

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

continents = ["Africa", "Europe"]
list2016 = ["1", "3"]
list2017 = ["4", "2"]
list2018 = ["14", "3"]

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

最佳答案

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")

enter image description here

关于python - 添加悬停以绘制具有多个垂直条的 Bokeh 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50937462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com