Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bar plot with groupby

My categorical variable case_satus takes on four unique values. I have data from 2014 to 2016. I would like to plot the distribution of case_status grouped by year. I try to this using:

df.groupby('year').case_status.value_counts().plot.barh() 

And I get the following plot:

output

What I would like to have is a nicer represenation. For example where I have one color for each year, and all the "DENIED" would stand next to each other.

I think it can be achieved since the groupby object is a multi-index, but I don't understand it well enough to create the plot I want.


The solution is:

df.groupby('year').case_status.value_counts().unstack(0).plot.barh() 

and results in

enter image description here

like image 678
jacob Avatar asked Jan 13 '18 08:01

jacob


People also ask

What is a grouped bar chart?

grouped bar charts are Bar charts in which multiple sets of data items are compared, with a single color used to denote a specific series across all sets. As with basic Bar charts, both vertical and horizontal versions of grouped bar charts are available.

How do I make a bar plot in pandas?

Traditionally, bar plots use the y-axis to show how values compare to each other. In order to make a bar plot from your DataFrame, you need to pass a X-value and a Y-value. Pandas will draw a chart for you automatically.


1 Answers

I think you need add unstack for DataFrame:

df.groupby('year').case_status.value_counts().unstack().plot.barh() 

Also is possible change level:

df.groupby('year').case_status.value_counts().unstack(0).plot.barh() 
like image 184
jezrael Avatar answered Oct 06 '22 12:10

jezrael