在Bokeh中的聚类条形图中包含工具提示

2024-06-12 05:24:42 发布

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

下面是一个聚集的条形图的一些代码。有谁能告诉我需要做些什么来包括下面的图表。它是一个聚集的(非堆叠)条形图。在

from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure

output_file("bar_stacked_grouped.html")

factors = [
    ("Q1", "jan"), ("Q1", "feb"), ("Q1", "mar"),
    ("Q2", "apr"), ("Q2", "may"), ("Q2", "jun"),
    ("Q3", "jul"), ("Q3", "aug"), ("Q3", "sep"),
    ("Q4", "oct"), ("Q4", "nov"), ("Q4", "dec"),

]

regions = ['east', 'west']

source = ColumnDataSource(data=dict(
    x=factors,
    east=[ 5, 5, 6, 5, 5, 4, 5, 6, 7, 8, 6, 9 ],
    west=[ 5, 7, 9, 4, 5, 4, 7, 7, 7, 6, 6, 7 ],
))

p = figure(x_range=FactorRange(*factors), plot_height=250,
           toolbar_location=None, tools="")

p.vbar_stack(regions, x='x', width=0.9, alpha=0.5, color=["blue", "red"], source=source,
             legend=[value(x) for x in regions])

p.y_range.start = 0
p.y_range.end = 18
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
p.legend.location = "top_center"
p.legend.orientation = "horizontal"

show(p)

谢谢

迈克尔


Tags: fromimportsourcevalueshowbokehrange条形图
2条回答

通过添加以下行,可以使用悬停工具显示一些数据:

source = ColumnDataSource(data=dict(
    x=factors,
    east=[ 5, 5, 6, 5, 5, 4, 5, 6, 7, 8, 6, 9 ],
    west=[ 5, 7, 9, 4, 5, 4, 7, 7, 7, 6, 6, 7 ],
))

tooltips = [
    ("x", "@x"),
    ("name", "$name"),
    ("value", "@$name")
]

p = figure(x_range=FactorRange(*factors), plot_height=250,
           toolbar_location="left", tools="hover", tooltips = tooltips)

悬停变量@$name可用于从堆栈列中查找每个层的值。例如,如果用户将鼠标悬停在名为“East”的堆栈glyph上,则@$name相当于@{East}。来源:https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#hover-tools

如果我理解您的意思,您需要添加工具提示:

所以你可以测试一下:

引用->;HoverTool and Tooltips

TOOLTIPS = [
    ("index", "$index"),
    ("(Q, east, west)", "([@x], @east, @west)"),
]

p = figure(x_range=FactorRange(*factors), plot_height=250, tooltips=TOOLTIPS,
           toolbar_location=None, tools="")

enter image description here

相关问题 更多 >