Reputation: 53
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:
Series structure with the values as number of people:
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:
What I want to get is something like this but with the x values as marital status:
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
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