2017-07-02 173 views
0

我要找的背景虛化的版本(採用直條)在matplotlib以下情節:堆積條形圖

import pandas as pd 
%matplotlib inline 

data = [ 
    ['201720', 'cat1', 20], 
    ['201720', 'cat2', 30], 
    ['201720', 'cat3', 40], 
    ['201721', 'cat1', 20], 
    ['201721', 'cat2', 0], 
    ['201721', 'cat3', 40],  
    ['201722', 'cat1', 50], 
    ['201722', 'cat2', 60], 
    ['201722', 'cat3', 10],  
] 

df = pd.DataFrame(data, columns=['week', 'category', 'count']) 

pt = df.pivot('week', 'category', 'count') 

pt.plot(kind='bar', stacked=True) 

enter image description here

我試着用搜索引擎,但我做不到找一個簡單的解決方案

回答

1

我認爲下面的代碼是我可以做的,現在最好的:

from bokeh.plotting import figure, output_file, show 
from bokeh.models import ColumnDataSource 
from bokeh.models.ranges import FactorRange 
import pandas as pd 

data = [ 
    ['201720', 'cat1', 20], 
    ['201720', 'cat2', 30], 
    ['201720', 'cat3', 40], 
    ['201721', 'cat1', 20], 
    ['201721', 'cat2', 0], 
    ['201721', 'cat3', 40], 
    ['201722', 'cat1', 50], 
    ['201722', 'cat2', 60], 
    ['201722', 'cat3', 10], 
] 

df = pd.DataFrame(data, columns=['week', 'category', 'count']) 

pt = df.pivot('week', 'category', 'count') 

pt = pt.cumsum(axis=1) 

output_file("lines.html", title='Dashboard') 

p = figure(title="count", 
      x_axis_label='week', y_axis_label='category', 
      x_range = FactorRange(factors=list(pt.index)), 
      plot_height=300, plot_width=500) 

p.vbar(x=pt.index, bottom=0, top=pt.cat1, width=0.2, color='red', legend='cat1') 
p.vbar(x=pt.index, bottom=pt.cat1, top=pt.cat2, width=0.2, color='blue', legend='cat2') 
p.vbar(x=pt.index, bottom=pt.cat2, top=pt.cat3, width=0.2, color='green', legend='cat3') 


show(p) 

產生的情節是這樣的:

enter image description here

包括直條(),背景虛化繪圖方法做似乎不支持「矢量化輸入」,或者我錯過了一些東西。這真的是最簡單的方法嗎?