Stack Bar Grapth with plot / pandas

时间:2016-08-31 12:07:41

标签: python plot

I am not being able to generate a stacked graph with the error below:

TypeError: There is no Line2D property "stacked"

My csv has the format:

feb-17,1,2,3
apr-17,2,4,3
may-18,3,5,3
oct-20,4,1,1
dec-21,5,1,1

I would expect to see something like:

enter image description here

My code is:

import pandas
import matplotlib.pyplot as plt


df = pandas.read_csv(datacsv.csv, delimiter=',', 
                 index_col=0, 
                 parse_dates=[0], dayfirst=True, 
                 names=['Date','Black','Red', 'Yellow'])

df.plot(Stacked=True, marker='.',markersize=8, title ="My graph", fontsize = 10, color=['b','g','c'], figsize=(15, 15))

imagepathprotocol = "image.png"

plt.savefig(imagepathprotocol)

Any idea what can be done? Thanks a lot.

2 个答案:

答案 0 :(得分:1)

尝试以下代码

df.plot(stacked=True,kind ='bar',title ="My graph",fontsize =10,color=['b','g','c'], figsize=(15, 15))

您需要指出栏类型并删除标记/标记化

答案 1 :(得分:0)

Yes, change the type -

df.plot.bar(stacked=True, marker='.',markersize=8, title ="My graph", fontsize = 10, color=['b','g','c'], figsize=(15, 15))

By default it tries to plot a 2D line and this indeed does not have a stacked option.

See documentation here