2017-07-02 174 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

包括直条(),背景虚化绘图方法做似乎不支持“矢量化输入”,或者我错过了一些东西。这真的是最简单的方法吗?