2016-09-26 74 views
2

我正在使用bokeh.plotting建设散景。我有两个系列的共享索引,我想绘制两个垂直条。当我使用单个工具条时,一切正常,但是当我添加第二个y范围和第二个工具栏时,它似乎正在影响主要y范围(将vales从0更改为4),而第二个vbar()覆盖第一个。值得赞赏的是,为什么酒吧重叠,而不是并排,为什么第二系列/ yaxis似乎影响第一,即使他们是分开的任何援助,将不胜感激。散焦次y范围影响主要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

+0

实现,我的条重叠由于共享索引,但不能与这里所示的例子中偏移它们:https://开头github上。 com/bokeh/bokeh/blob/master/examples/plotting/file/bar_chart.py,因为我使用字符串的分类轴。任何抵消这些想法将不胜感激。也可以通过硬编码修复主要的y轴范围,但希望避免这种情况。 – Alexander

回答

3

这是我相信你想要的情节: 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中的分类轴目前有点(ahem)丑陋。我们希望在未来几个月内解决这个问题。每个人在冒号后都有一个0-1的刻度,可以让你左右移动。因此,我将第一个条向左移动0.3/2,第二个条向右移动0.3/2(0.3,因为这是您使用的宽度)
  • y_range发生更改,因为您使用默认的y_range作为初始值y_range是一个DataRange1d。 DataRange使用图的所有数据来选择它的值并添加一些填充,这就是为什么它从0开始并且达到新数据的最大值。通过手动指定图中的范围,可以解决这个问题。

感谢您提供一个代码示例工作从:d

+0

这是刚刚出来的0.12.3。 – birdsarah

+0

谢谢你这个答案正是我一直在试图用这个。我将努力将其与我一直试图用Bokeh动态创建的一些不同图形进行整合。 – Alexander

+0

FWIW:此解决方案似乎不适用于更新版本的Bokeh。 (我看到它在0.12.10中突破)我试图弄清楚什么改变了,以及这种行为是否仍然支持。我会回来的w /更新我发现,但如果有人比我更了解我同时我会喜欢一个指向正确的方向。 :) –