HeftyDolphin
HeftyDolphin

Reputation: 53

Matplotlib bar chart that displays the x values for male and females

I want to try analyse the distribution of marital status by gender and I feel a bar chart is the best to way to go. I can figure out how to get the number of people per marital status but can't figure out how to split the result for male and female for each status. I've watched a lot of tutorials for bar charts in matplotlib using pandas but I can't seem to get it.

Dataframe:

enter image description here

Series structure with the values as number of people:

Series

This is what I did to plot for just the number of people per status:

series = dfFile["marital_status"].value_counts()

series.plot('bar')
plt.show()

What I'm getting:

Graph displayed

What I want to get is something like this but with the x values as marital status:

enter image description here

Quite new to Python and Matplotlib so I appoligise if the answer is obvious or if the question isn't worded very well.

Upvotes: 2

Views: 5581

Answers (1)

Brandon
Brandon

Reputation: 1018

This should help:

gender_column = <ENTER GENDER COLUMN NAME HERE>
dfFile.groupby(["marital_status", gender_column]).size().unstack(level=1).plot(kind='bar')

Upvotes: 2

Related Questions