2017-05-12 68 views
1

是什麼力量讓一個100%堆積條形圖中Bokeh?,例如,假設我有以下幾列的最簡單的方法散景100%堆疊條形圖

S P 
34 65 
23 44 
12 81 
9 23 

excel我可以做這種類型的情節很容易的,所以我會得到這樣的事情:

enter image description here

不過,我想有一些互動(如顯示懸停值),因此,我想做出這種情節在Bokeh。我是Bokeh的初學者,我還沒有找到類似於此的任何示例。那麼,最好的方法是什麼?

回答

3

數據扯皮

df_comb = df.join(df.divide(df.sum(axis=1), axis=0), rsuffix='_w').join(df.divide(df.sum(axis=1) * 2, axis=0), rsuffix='_w_labelheights') 
df_comb['P_w_labelheights'] += df_comb['S_w'] 
df_comb 

,以獲得正確的比例和標籤高度

S P S_w   P_w   S_w_labelheights P_w_labelheights 
0 34 65 0.343434 0.656566 0.171717 0.671717 
1 23 44 0.343284 0.656716 0.171642 0.671642 
2 12 81 0.129032 0.870968 0.064516 0.564516 
3 9 23 0.281250 0.718750 0.140625 0.640625 

背景虛化開始

的筆記本

from bokeh.models import ColumnDataSource 
from bokeh.plotting import show, output_notebook, figure as bf 
output_notebook() 

情節創作

f = bf() 
source = ColumnDataSource(df_comb) 

s = f.vbar(x='index', bottom=0, top='S_w', width=0.5, source=source) 
p = f.vbar(x='index', bottom='S_w', top=1, width=0.5, source=source, color='orange') 

s_label = f.text(x='index', y='S_w_labelheights', source=source, text='S') 
p_label = f.text(x='index', y='P_w_labelheights', source=source, text='P') 

show(f) 

enter image description here

你可以在後面加上這個HoverTool和糾正ticksgrid

+0

加入'text_align ='center''到'f.text()'呼叫中心的文本,如果這您更喜歡什麼 –

+1

參考文檔可以是[這裏](http://bokeh.pydata.org/en/latest/docs/reference/plotting.html) –

+0

非常感謝您的先生,這個例子幫助我許多。 – Xhoan