簡體   English   中英

具有標稱/分類x軸+雙y軸的Python散景條形圖

[英]Python bokeh bar chart with nominal / categorical x-axis + twin y-axis

如何使用bokeh繪制條形圖,並具有左右y-axis 我想從數據框中繪制兩列,其中行ID是分類的,應該是x-axis 僅向一個y-axis extra_y_rangesBar效果很好,但我不知道如何向條形圖添加extra_y_ranges ,或者更好的說法是我可以創建extra_y_ranges但不能在條形圖上使用add_layout 而且我無法以第一列與左側y-axis且第二列與右側y-axis相關的方式繪制數據框列。

我嘗試使用figure.quad手動創建條形圖。 我必須使用x_range=my_df.index因為我需要分類的x-axis 我使用相同的值,在leftright參數和I使用line_width=7不具有單一的線,但一個欄。 但由於我想將第二個屬性繪制在相同的類別ID上,因此我不得不將條形向左移一點。 所以我的問題是:如何將圖向左/向右移動?

這是我的示例示例:

from bokeh.plotting import figure, output_file, show
from bokeh.models import LinearAxis, Range1d

output_file("bars.html")

p = figure(title="twin y-axis bar example", x_range=['0','4','2'])

p.quad(bottom=0, top=[70,60,50], left=['0','4','2'], right=['0','4','2']
    , line_width=7, line_color='red')

p.extra_y_ranges = {'foo':Range1d(start=0, end=20)}
p.add_layout(LinearAxis(y_range_name='foo', axis_label='right_y_axis'), place='right')

p.quad(bottom=0, top=[15,10,5], left=['0','4','2'], right=['0','4','2']
    , line_width=7, line_color='blue', y_range_name='foo')

show(p)

所以我想向左移動紅色條,向右移動藍色條,以免彼此覆蓋。

謝謝!!!

我能夠以一種可怕的方式“解決”這個問題:我創建了許多空類別(名稱中空格的數量不同)。 在我的示例中,它看起來不太好,但是如果有人擁有更多類別(例如,> 10),則它看起來可能像真實的普通條形圖。 當然,可以使用數據框方法輕松生成這些空類別,但是在我的示例代碼中,我僅使用列表。 這是我的代碼:

from bokeh.plotting import figure, output_file, show
from bokeh.models import LinearAxis, Range1d

output_file("bars.html")

p = figure(title="twin y-axis bar example", x_range=['','0','',' ','4',' '
    ,'  ','2','  ','   ','7','   ','    ','3','    ','     ','1','     '])

p.quad(bottom=0, top=[70,68,50,48,30,28], left=['',' ','  ','   ','    ','     ']
    , right=['',' ','  ','   ','    ','     '], line_width=7, line_color='red')

p.yaxis.axis_label = 'left y axis'
p.yaxis.axis_label_text_color = 'red'

p.extra_y_ranges = {'foo':Range1d(start=-1, end=21)}
p.add_layout(LinearAxis(y_range_name='foo', axis_label='right y axis'
    , axis_label_text_color='blue'), place='right')

p.quad(bottom=0, top=[18,15,12,9,6,3], left=['0','4','2','7','3','1']
    , right=['0','4','2','7','3','1'], line_width=7, line_color='blue', y_range_name='foo')

p.xaxis.axis_label = 'x axis'
p.xaxis.axis_label_standoff = -5

show(p)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM