2016-09-26 77 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 /更新我發現,但如果有人比我更瞭解我同時我會喜歡一個指向正確的方向。 :) –