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

时间:2016-09-08 23:11:31

标签: python bokeh

在Bokeh指南中,可以创建各种条形图的示例。 http://bokeh.pydata.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)

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

2 个答案:

答案 0 :(得分:12)

使用Labelset

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

在我的示例中,我使用带有绘图界面的vbar,它比Charts界面稍微低一点,但可能有办法将其添加到条形图中。

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

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

答案 1 :(得分:5)

从BOKEH MAINTAINERS注意以下答案中引用bokeh.charts的部分仅具有历史意义。 bokeh.charts API已弃用,随后从Bokeh中移除。有关稳定的bokeh.plotting API

的信息,请参阅此处及上方的答案

是的,您可以为图表的每个条添加标签。有几种方法可以做到这一点。默认情况下,您的标签与您的数据相关联。但您可以更改显示的内容。以下是使用您的示例执行此操作的几种方法:

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

from pandas import DataFrame
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import Range1d, HoverTool

# output_file("bar.html")

""" Adding some sample labels a few different ways.
    Play with the sample data and code to get an idea what does what.
    See below for output.
"""

示例数据(新标签):

我使用了一些逻辑来确定新的dataframe列。当然,您可以使用df中已有的其他列(这完全取决于您正在使用的数据)。这里你真正需要的是为数据帧提供一个新列。

# One method
labels = []
for number in df['cyl']:
    if number == 3:
        labels.append("three")
    if number == 4:
        labels.append("four")
    if number == 5:
        labels.append("five")
    if number == 6:
        labels.append("six")
    if number == 8:
        labels.append("eight")

df['labels'] = labels

获取新数据框列的另一种方法。同样,我们只需要在条形图上提供df一个新列。

# Another method
def new_labels(x):
    if x % 2 != 0 or x == 6:
        y = "Inline"
    elif x % 2 == 0:
        y = "V"
    else:
        y = "nan"
    return y

df["more_labels"] = df["cyl"].map(new_labels)

现在是条形图:

我已经两种方式完成了它。 p1只是指定新标签。请注意,因为我使用了字符串,所以它们按字母顺序排列在图表上。 p2使用原始标签,并在同一个栏上添加我的新标签。

# Specifying your labels
p1 = Bar(df, label='labels', values='mpg',
         title="Total MPG by CYL, remapped labels, p1",
         width=400, height=400, legend="top_right")
p2 = Bar(df, label=['cyl', 'more_labels'], values='mpg',
         title="Total MPG by CYL, multiple labels, p2", width=400, height=400,
         legend="top_right")

另一种方式:

Bokeh有三个主要的接口级别"。高级charts提供快速轻松访问但功能有限; plotting提供了更多选择; models提供了更多选择。

我在这里使用绘图界面和包含Figure方法的rect类。这样可以更详细地控制图表。

# Plot with "intermediate-level" bokeh.plotting interface
new_df = DataFrame(df.groupby(['cyl'])['mpg'].sum())
factors = ["three", "four", "five", "six", "eight"]
ordinate = new_df['mpg'].tolist()
mpg = [x * 0.5 for x in ordinate]

p3 = figure(x_range=factors, width=400, height=400,
            title="Total MPG by CYL, using 'rect' instead of 'bar', p3")
p3.rect(factors, y=mpg, width=0.75, height=ordinate)
p3.y_range = Range1d(0, 6000)
p3.xaxis.axis_label = "x axis name"
p3.yaxis.axis_label = "Sum(Mpg)"

添加特定标签的第四种方式:

我在这里使用hover绘图工具。将鼠标悬停在每个栏上以显示指定的标签。

# With HoverTool, using 'quad' instead of 'rect'
top = [int(x) for x in ordinate]
bottom = [0] * len(top)
left = []
[left.append(x-0.2) for x in range(1, len(top)+1)]
right = []
[right.append(x+0.2) for x in range(1, len(top)+1)]
cyl = ["three", "four", "five", "six", "eight"]
source = ColumnDataSource(
    data=dict(
        top=[int(x) for x in ordinate],
        bottom=[0] * len(top),
        left=left,
        right=right,
        cyl=["three", "four", "five", "six", "eight"],
    )
)

hover = HoverTool(
    tooltips=[
        ("cyl", "@cyl"),
        ("sum", "@top")
    ]
)

p4 = figure(width=400, height=400,
            title="Total MPG by CYL, with HoverTool and 'quad', p4")
p4.add_tools(hover)
p4.quad(top=[int(x) for x in ordinate], bottom=[0] * len(top),
        left=left, right=right, color="green", source=source)
p4.xaxis.axis_label = "x axis name"

在网格中显示所有四个图表:

grid = gridplot([[p1, p2], [p3, p4]])
show(grid)

这些是我所知道的方式。可能还有其他人。改变你喜欢的任何东西以满足你的需求。以下是运行所有这些输出的内容(您必须运行它或者为了获得hovertool而提供它):

bokeh bar specify labels bokeh bar specify labels