如何在Bokeh中向条形图添加数据标签? [英] How to add data labels to a bar chart in Bokeh?

查看:459
本文介绍了如何在Bokeh中向条形图添加数据标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the Bokeh guide there are examples of various bar charts that can be created. 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)

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

My question is if it's possible to add data labels to each individual bar of the chart? I searched online but could not find a clear answer.

推荐答案

使用标签集

使用Labelset在每个单独的条形上方创建标签

Use Labelset

Use Labelset to create a label over each individual bar

在我的示例中,我将vbar与绘图界面一起使用,它的水平比Charts界面低一些,但是可能有一种方法可以将其添加到条形图中.

In my example I'm using vbar with the plotting interface, it is a little bit more low level then the Charts interface, but there might be a way to add it into the Bar chart.

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)

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

You can find more about labelset here: Bokeh annotations

这篇关于如何在Bokeh中向条形图添加数据标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆