Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Bokeh hbar和ColumnDataSource逐个标记_Python_Pandas_Bokeh - Fatal编程技术网

Python 使用Bokeh hbar和ColumnDataSource逐个标记

Python 使用Bokeh hbar和ColumnDataSource逐个标记,python,pandas,bokeh,Python,Pandas,Bokeh,我有一个hbar-图,由ColumnDataSource驱动,在y轴和两个数字x轴上有分类标签。为了在y轴上每个标签有两组条形图,我构造了两个范围(Things1=df.Thing.index+1-0.2和Things2=df.Thing.index+1+0.2),并将两个hbar-实例分别分配给其中一个范围 当且仅当ColumnDataSource中的范围Things1和Things2以预定义的偏移量1构建时,这为我提供了正确的布局;否则,所有标签和条形图只偏移一个条目 我现在构建绘图的方式如

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

当且仅当
ColumnDataSource
中的范围
Things1
Things2
以预定义的偏移量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
给出了正确的绘图:

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


我的猜测是,这是因为不同的范围是以0或1为基础的。这是一个简单的问题,还是我做错了?

编辑结果表明,在因子范围内使用原始值(与真正的分类数据相反)是没有记录和未经测试的。分类数据的处理将在0.12.7中更改,这将允许正确使用分类数据所需的功能-请参阅下面的bigreddots(bokeh开发负责人)评论。

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

如果在初始化打印对象后添加y_范围,则会显示原始刻度:

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

如果查看例如的示例,可以看到数据映射到类别,但这是因为y值本身是作为因子输入的。在您的示例中,您只是更改y轴,但要偏移值,您不能使用原始类别本身

旁白: 编辑偏移属性在0.12.7中可能不存在,因此最好不要依赖它/假设它将存在。

查看文档时,默认偏移量为0。所以我想你会想要一个-1的偏移量来解释这个。看


请注意,在下一个Bokeh版本中,分类的处理正在更改,
offset
属性可能会被完全删除。官方支持的提供分类坐标的方法是使用实际的类别,而不是直接尝试给出合成坐标。这样做是偶然的,没有文档记录。所以最好的做法是不使用因子范围,并使用FuncTickFormatter映射y轴标签?然后,数据不是真正的分类数据,只是实现了y轴的重新标记。否则,将需要像海报所尝试的那样抵消实际值。没有理由不使用真正的分类,即
FactorRange
。分类可以在
0.12.6
和更早版本中使用goofy
“foo:0.2”
语法进行偏移。在
0.12.7
中,将支持真正的嵌套分类,这样就根本不需要为了得到分组而进行偏移,即使需要进行偏移,也将使用
[“foo”,0.2]
这是一种更为强大的语法。但是
0.12.7
还将增加对在类别组之间添加填充的支持(例如,使分组的条形图可以更接近y组),在这种情况下,从类别坐标到合成坐标的映射根本就不是那么简单(也在浏览器中计算),所以这是另一个喜欢使用实数因子作为分类坐标的原因。酷,如果更深入的话,我会删除我的。最后要补充的是,re:FuncTickFormatter也可以是一个OK路线,但我个人只有在我想要完全控制从“类别”到“合成坐标”的映射时才会这样做。我不认为这是第一个达到的方法。嗨,我的问题是隐藏轴,如“叶子”,“草”。。。Bokeh ColumnDataSource中有什么解决方案吗?
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)