Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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        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: enter image description here

Any idea on how to fix it? I am doing something wrong with the groupby, but not sure of the solution

like image 940
user308827 Avatar asked Oct 31 '14 21:10

user308827


People also ask

What is the Order of a stacked bar chart in pandas?

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.

How to create a stacked bar chart in Excel?

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.

How do I plot a bar chart in pandas?

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!)

What is the difference between stacked and unstacked bar charts?

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.


1 Answers

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.

like image 194
Simon Avatar answered Nov 03 '22 06:11

Simon