Bokeh次要y轴范围如何影响主要y轴范围

3

我正在使用bokeh.plotting构建Bokeh图表。我有两个具有共享索引的系列,希望绘制两个垂直条形图。当我只使用一个条形图时,一切工作正常,但是当我添加第二个y轴范围和第二个条形图时,它似乎会影响主要的y轴范围(将值从0到4更改),而我的第二个vbar()会叠加在第一个上面。如果您能就为什么这些条形图重叠而不是并排以及为什么第二个系列/y轴似乎会影响第一个,尽快提供帮助,将不胜感激。

import pandas as pd
import bokeh.plotting as bp
from bokeh.models import NumeralTickFormatter, HoverTool, Range1d, LinearAxis

df_x_series = ['a','b','c']
fig = bp.figure(title='WIP',x_range=df_x_series,plot_width=1200,plot_height=600,toolbar_location='below',toolbar_sticky=False,tools=['reset','save'],active_scroll=None,active_drag=None,active_tap=None)
fig.title.align= 'center'
fig.extra_y_ranges = {'c_count':Range1d(start=0, end=10)}
fig.add_layout(LinearAxis(y_range_name='c_count'), 'right')
fig.vbar(bottom=0, top=[1,2,3], x=['a','b','c'], color='blue', legend='Amt', width=0.3, alpha=0.5)
fig.vbar(bottom=0, top=[5,7,8], x=['a','b','c'], color='green', legend='Ct', width=0.3, alpha=0.8, y_range_name='c_count')
fig.yaxis[0].formatter = NumeralTickFormatter(format='0.0')
bp.output_file('bar.html')
bp.show(fig)

bokeh plot


意识到我的条形图由于共享索引而重叠,但无法像这里显示的示例(https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/bar_chart.py)一样使用字符串的分类轴来偏移它们。如果有任何偏移这些的想法将不胜感激。也可以通过硬编码来修复主要y轴范围,但希望避免这样做。 - Alexander
1个回答

4
这是我认为你想要的情节: enter image description here 这是代码:
import bokeh.plotting as bp
from bokeh.models import NumeralTickFormatter, Range1d, LinearAxis

df_x_series = ['a', 'b', 'c']
fig = bp.figure(
    title='WIP',
    x_range=df_x_series,
    y_range=Range1d(start=0, end=4),
    plot_width=1200, plot_height=600,
    toolbar_location='below',
    toolbar_sticky=False,
    tools=['reset', 'save'],
    active_scroll=None, active_drag=None, active_tap=None
)
fig.title.align = 'center'
fig.extra_y_ranges = {'c_count': Range1d(start=0, end=10)}
fig.add_layout(LinearAxis(y_range_name='c_count'), 'right')
fig.vbar(bottom=0, top=[1, 2, 3], x=['a:0.35', 'b:0.35', 'c:0.35'], color='blue', legend='Amt', width=0.3, alpha=0.5)
fig.vbar(bottom=0, top=[5, 7, 8], x=['a:0.65', 'b:0.65', 'c:0.65'], color='green', legend='Ct', width=0.3, alpha=0.8, y_range_name='c_count')
fig.yaxis[0].formatter = NumeralTickFormatter(format='0.0')
bp.output_file('bar.html')
bp.show(fig)

一些注意事项:

  • 目前在 Bokeh 中,分类轴有点丑陋。我们希望在未来几个月内解决这个问题。每个轴都有一个 0 - 1 的比例尺,允许您将物体向左或向右移动。所以我通过将第一个柱形图向左移动 0.3/2,将第二个柱形图向右移动 0.3/2(0.3 是您使用的宽度)。
  • y_range 发生了变化,因为您在初始 y_range 中使用了默认的 DataRange1d。DataRange 使用绘图的所有数据选择其值并添加一些填充,这就是为什么它从低于 0 开始并上升到新数据的最大值。通过在 figure 调用中手动指定范围,可以解决此问题。

感谢提供可供参考的代码示例 :D


这是用刚发布的0.12.3版本制作的。 - birdsarah
谢谢,这个答案正是我一直在尝试的。我会努力将其与我正在尝试使用Bokeh动态创建的其他图形集成在一起。 - Alexander
值得一提的是:这个解决方案似乎不能与较新版本的Bokeh一起使用。(我在0.12.10中看到它出现了故障。)我正在努力弄清楚发生了什么变化,以及是否仍然支持这种行为。我会回来更新我找到的内容,但如果有人在此期间比我更了解,请指点一下正确的方向。 :) - Cody Casterline
更新:进一步调查表明,这个示例代码(以及我们基于它的代码)在0.12.7版本中开始出现问题。似乎有一个新的“视觉躲避”功能被添加/记录了下来。也许这是新的首选方式? https://bokeh.pydata.org/en/latest/docs/user_guide/categorical.html#visual-dodge - Cody Casterline

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接