Sunday 15 July 2012

python - making a stacked barchart in pandas -



python - making a stacked barchart in pandas -

i create stacked bar plot next dataframe:

value count recl_lcc recl_pi 0 1 15686114 3 1 1 2 27537963 1 1 2 3 23448904 1 2 3 4 1213184 1 3 4 5 14185448 3 2 5 6 13064600 3 3 6 7 27043180 2 2 7 8 11732405 2 1 8 9 14773871 2 3

there 2 bars in plot. 1 recl_lcc , other recl_pi. there 3 sections in each bar corresponding unique values in recl_lcc , recl_pi i.e 1,2,3 , sum count each section. far, have this:

df = df.convert_objects(convert_numeric=true) sub_df = df.groupby(['recl_lcc','recl_pi'])['count'].sum().unstack() sub_df.plot(kind='bar',stacked=true)

however, plot:

any thought on how prepare it? doing wrong groupby, not sure of solution

i've set info shown in stackpandas.dat. given data:

from pandas import * import matplotlib.pyplot 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:

... think sought.

python pandas plot stacked

No comments:

Post a Comment