gpt4 book ai didi

python - 如何在 Bokeh 的条形图中添加数据标签?

转载 作者:太空狗 更新时间:2023-10-30 01:42:38 25 4
gpt4 key购买 nike

在 Bokeh 指南中有可以创建的各种条形图的示例。 http://docs.bokeh.org/en/0.10.0/docs/user_guide/charts.html#id4

此代码将创建一个:

from bokeh.charts import Bar, output_file, show
from bokeh.sampledata.autompg import autompg as df

p = Bar(df, 'cyl', values='mpg', title="Total MPG by CYL")

output_file("bar.html")

show(p)

我的问题是是否可以为图表的每个单独的条形图添加数据标签?我在网上搜索但找不到明确的答案。

最佳答案

使用标签集

使用 Labelset 在每个单独的栏上创建标签

在我的示例中,我将 vbar 与绘图界面一起使用,它比图表界面低一点,但可能有一种方法可以将它添加到条形图中。

from bokeh.palettes import PuBu
from bokeh.io import show, output_notebook
from bokeh.models import ColumnDataSource, ranges, LabelSet
from bokeh.plotting import figure
output_notebook()

source = ColumnDataSource(dict(x=['Áætlaðir','Unnir'],y=[576,608]))

x_label = ""
y_label = "Tímar (klst)"
title = "Tímar; núllti til þriðji sprettur."
plot = figure(plot_width=600, plot_height=300, tools="save",
x_axis_label = x_label,
y_axis_label = y_label,
title=title,
x_minor_ticks=2,
x_range = source.data["x"],
y_range= ranges.Range1d(start=0,end=700))


labels = LabelSet(x='x', y='y', text='y', level='glyph',
x_offset=-13.5, y_offset=0, source=source, render_mode='canvas')

plot.vbar(source=source,x='x',top='y',bottom=0,width=0.3,color=PuBu[7][2])

plot.add_layout(labels)
show(plot)

Bar chart with labels on top of each bar

您可以在此处找到有关标签集的更多信息:Bokeh annotations

关于python - 如何在 Bokeh 的条形图中添加数据标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39401481/

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