Home > database >  how to create a stacked bar chart with matplotlib?
how to create a stacked bar chart with matplotlib?

Time:09-25

I have a pandas dataframe with some sample data as follows

   year_start       party  num_legistators
0         2010    democrat               50
1         2010  republican              133
2         2010     unknown                4
3         2011    democrat               67
4         2011  republican              56
5         2012    democrat               76
6         2012  republican              43

I am trying to create a stacked bar chart where the x axis is the year_start and the y axis is the num_legislators and the color of the stacked bars is party

I have done this so far

df_toviz.plot(x= "year_start", kind='bar', stacked=True)

enter image description here

how do I get to be stacked ?

CodePudding user response:

You need to pivot your data frame first so party goes as columns:

enter image description here

  • Related