用于条形图的Bokeh标签和悬停工具,python

2024-09-29 17:15:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图建立一个条形图,其中我有三个图例(所以三个不同的条形图)与一个鼠标悬停工具相结合

目前我已经用图例构建了条形图,但是我在使用HoverTool时遇到了问题。在下面显示的代码中,悬停工具显示所有图表的所有三个工具提示。我只希望悬停工具显示其中一个工具提示,因此,例如,对于条形图“popPerc”,我只希望在悬停工具中看到“@population”

p = figure(x_range=df2.district,plot_height=300,plot_width=500,
           y_range= ranges.Range1d(start=0,end=25))

p.xgrid.grid_line_color=None
p.ygrid.grid_line_color=None
p.y_range.start = 0
p.xaxis.major_label_orientation = 0.5
p.yaxis.visible = False
p.toolbar_location=None
p.outline_line_color = None
colors = all_palettes['BuGn'][3]
bar = {}
items = []
color = 0

features = ['popPerc','areaPerc','treesPerc']

for indx,i in enumerate(features):
    bar[i] = p.vbar(x='district', top=i, source=df2, muted_alpha=0, muted=False,
                   width= 0.8, color=colors[color])
    items.append((i,[bar[i]]))
    color+=1

legend = Legend(items=items,location=(0,100))
p.add_tools(HoverTool(tooltips = [('Trees','@trees'),
                                  ('Population','@population'),
                                  ('Area [km^2]','@area')]))
p.add_layout(legend,'left')
p.legend.click_policy='hide'
show(p)

希望有人能帮忙,提前谢谢!:)


Tags: 工具nonelinebaritemsrangecolor条形图
1条回答
网友
1楼 · 发布于 2024-09-29 17:15:55

读完这篇文章后,我明白了。通过将hovertool的代码块更改为以下代码块,它可以工作

p.add_tools(HoverTool(renderers=[items[0][1][0]], tooltips = [('Population','@population')]))
p.add_tools(HoverTool(renderers=[items[1][1][0]], tooltips = [('Area [km^2]','@area')]))
p.add_tools(HoverTool(renderers=[items[2][1][0]], tooltips = [('Trees','@trees @treePerc')]))

相关问题 更多 >

    热门问题