Skip to content Skip to sidebar Skip to footer

Making A Stacked Barchart In Pandas

I would like to create a stacked bar plot from the following dataframe: VALUE COUNT RECL_LCC RECL_PI 0 1 15686114 3 1 1 2 27537963 1

Solution 1:

I've put data shown in stackpandas.dat. Given those data:

from pandas import *
import matplotlib.pyplot as plt

df = read_table("stackpandas.dat"," +",engine='python')

df = df.convert_objects(convert_numeric=True)
sub_df1 = df.groupby(['RECL_LCC'])['COUNT'].sum()
sub_df2 = df.groupby(['RECL_PI'])['COUNT'].sum()
sub_df = concat([sub_df1,sub_df2],keys=["RECL_LCC","RECL_PI"]).unstack()
sub_df.plot(kind='bar',stacked=True,rot=1)
plt.show()

... gives: enter image description here

... which I think is what is sought.

Post a Comment for "Making A Stacked Barchart In Pandas"