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

spa*_*row 6 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)
Run Code Online (Sandbox Code Playgroud)

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

sig*_*rdb 13

使用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)
Run Code Online (Sandbox Code Playgroud)

条形图与标签在每个酒吧的顶部

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

  • 将 `text_align='center'` 添加到 LabelSet 构造函数中。 (2认同)

小智 5

来自BOKEH MAINTAINERS注释以下答案的部分bokeh.charts仅涉及历史.该bokeh.chartsAPI已弃用,随后从Bokeh中删除.有关稳定bokeh.plottingAPI的信息,请参阅此处及上方的答案


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

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.
"""
Run Code Online (Sandbox Code Playgroud)

样本数据(新标签):

我使用了一些逻辑来确定新的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
Run Code Online (Sandbox Code Playgroud)

获取新数据帧列的另一种方法.同样,我们只需要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)
Run Code Online (Sandbox Code Playgroud)

现在条形图:

我有两种方式.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")
Run Code Online (Sandbox Code Playgroud)

其他方式:

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)"
Run Code Online (Sandbox Code Playgroud)

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

我在这里使用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"
Run Code Online (Sandbox Code Playgroud)

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

grid = gridplot([[p1, p2], [p3, p4]])
show(grid)
Run Code Online (Sandbox Code Playgroud)

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

散景栏指定标签 散景栏指定标签