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 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 would be 2 bars in the plot. One for RECL_LCC
and other for RECL_PI
. There would be 3 sections in each bar corresponding to the unique values in RECL_LCC
and RECL_PI
i.e 1,2,3 and would sum up the COUNT
for each section. So far, I have something like 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, I get this plot:
Any idea on how to fix it? I am doing something wrong with the groupby
, but not sure of the solution
Stacked Bars in Order. The order of the bars in the stacked bar chart is determined by the order of the columns in the Pandas dataframe. In the stacked bar chart, we’re seeing total number of pies eaten over all years by each person, split by the years in question.
Setting parameter stacked to True in plot function will change the chart to a stacked bar chart. To create a cumulative stacked bar chart, we need to use groupby function again: We group by level= [1] as that level is Type level as we want to accumulate sales by type. To create horizontal bar charts, we just need to change chart kind to barh.
This plot is easily achieved in Pandas by creating a Pandas “Series” and plotting the values, using the kind="bar" argument to the plotting command. For example, say you wanted to plot the number of mince pies eaten at Christmas by each member of your family on a bar chart. (I have no idea why you’d want to do that!)
In the stacked version of the bar plot, the bars at each index point in the unstacked bar chart above are literally “stacked” on top of one another.
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:
... which I think is what is sought.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With