gpt4 book ai didi

python - 与 Bokeh hbar 和 ColumnDataSource 的标签相差一

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:01 27 4
gpt4 key购买 nike

我有一个由 ColumnDataSource 驱动的 hbar 图,其中 y 轴上有分类标签,两个数字 x 轴上有分类标签。为了在 y 轴上每个标签有两组条形,我构造了两个范围 (Things1=df.Thing.index + 1 - 0.2Things2=df.Thing.index + 1 + 0.2) 并将两个 hbar 实例分别分配给这些范围之一。

当且仅当 ColumnDataSource 中的范围 Things1Things2 是使用预定义偏移量 1 构造的时,这才给我正确的布局;否则,所有标签和条形图都恰好偏移一个条目。

我现在构建绘图的方式如下所示:

df = pandas.DataFrame([('Store', 'Juice', 3, 19.0),
('Warehouse', 'Paint', 7, 21.0),
('Store', 'Fruit', 2, 6.0),
('Warehouse', 'Grass', 4, 15.0),
('Store', 'Leaves', 9, 32.0)],
columns=('Storage', 'Thing', 'Quantity', 'Valueation'))


source = bokeh.models.ColumnDataSource(dict(Quantity=df.Quantity,
Things1=df.Thing.index + 1 - 0.2, # Why +1 ?
Things2=df.Thing.index + 1 + 0.2, # Why +1 ?
Valueation=df.Valueation,
Storage=df.Storage))

plot = bokeh.plotting.figure(plot_width=800, plot_height=300,
y_range=list(df.Thing.unique()),
x_range=(0, df.Quantity.max() * 1.1))
plot.hbar(y='Things1', right='Quantity', height=0.3, alpha=0.7, source=source)
plot.hbar(y='Things2', right='Valueation', height=0.3, alpha=1, source=source,
x_range_name="ValueationRange")
plot.yaxis.axis_label = 'Thing'
plot.xaxis.axis_label = 'Quantity'
plot.extra_x_ranges = {"ValueationRange":
bokeh.models.Range1d(start=0, end=df.Valueation.max() * 1.1)}
plot.add_layout(bokeh.models.LinearAxis(x_range_name="ValueationRange",
axis_label='Valueation'), 'above')
bokeh.plotting.show(plot)

Things1/2=df.Thing.index + 1 -/+ 0.2 给出正确的绘图:

Correct plot

使用 Things1/2=df.Thing.index -/+ 0.2 我得到

Wrong plot

我的猜测是,这是由于不同的范围是基于 0 或 1 的。这是一个简单的陷阱还是我做错了?

最佳答案

编辑事实证明,这种使用原始值(而不是真实的分类数据)和因子范围的方法没有记录和未经测试。分类数据的处理将在 0.12.7 中发生变化,这将允许正确使用分类数据所需的功能 - 请参阅下面的 bigreddots(主要 Bokeh 开发人员)评论。

仅从观察来看,分类范围的默认行为似乎是将整数 y 值从 1 开始映射到类别。

如果在初始化绘图对象后添加 y_range,则会暴露原始刻度: plot no label

正如您所提到的,这只是由于范围所致。第一个绘制的数据系列从 0 开始,但第一个标签从 1 开始。您可以使用 funtickformatter 设置偏移量或将显式 y 刻度映射到值,这可以精确控制值和标签之间的映射。

如果你看一个例子,例如 http://docs.bokeh.org/en/latest/docs/gallery/categorical.html ,您可以看到数据映射到类别,但这是因为 y 值本身作为因子输入。在您的示例中,您只是更改 y 轴,但为了偏移值,您不能使用原始类别本身。

顺便说一句:编辑 offset 属性在 0.12.7 中可能不存在,因此最好不要依赖它/假设它存在。

查看文档,它说默认偏移量为 0。所以我认为您需要 -1 的偏移量来解释这一点。请参阅http://docs.bokeh.org/en/latest/docs/reference/models/ranges.html#bokeh.models.ranges.FactorRange.offset

import pandas

import bokeh
import bokeh.models, bokeh.plotting

df = pandas.DataFrame([('Store', 'Juice', 3, 19.0),
('Warehouse', 'Paint', 7, 21.0),
('Store', 'Fruit', 2, 6.0),
('Warehouse', 'Grass', 4, 15.0),
('Store', 'Leaves', 9, 32.0)],
columns=('Storage', 'Thing', 'Quantity', 'Valueation'))


source = bokeh.models.ColumnDataSource(dict(Quantity=df.Quantity,
Things1=df.Thing.index - 0.2, # Why +1 ?
Things2=df.Thing.index + 0.2, # Why +1 ?
Valueation=df.Valueation,
Storage=df.Storage))

plot = bokeh.plotting.figure(plot_width=800, plot_height=300,
y_range=list(df.Thing.unique()),
x_range=(0, df.Quantity.max() * 1.1))

plot.y_range.offset = -1

plot.hbar(y='Things1', right='Quantity', height=0.3, alpha=0.7, source=source)
plot.hbar(y='Things2', right='Valueation', height=0.3, alpha=1, source=source,
x_range_name="ValueationRange")
plot.yaxis.axis_label = 'Thing'
plot.xaxis.axis_label = 'Quantity'
plot.extra_x_ranges = {"ValueationRange":
bokeh.models.Range1d(start=0, end=df.Valueation.max() * 1.1)}
plot.add_layout(bokeh.models.LinearAxis(x_range_name="ValueationRange",
axis_label='Valueation'), 'above')
bokeh.plotting.show(plot)

关于python - 与 Bokeh hbar 和 ColumnDataSource 的标签相差一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45280354/

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