Matplotlib条形图显示男性和女性的x值 [英] Matplotlib bar chart that displays the x values for male and females

查看:93
本文介绍了Matplotlib条形图显示男性和女性的x值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试按性别分析婚姻状况的分布,我认为条形图是最好的选择.我可以弄清楚如何获得每个婚姻状况的人数,但不能弄清楚如何针对每个状况将男性和女性的结果分开.我已经看过很多使用pandas在matplotlib中使用条形图的教程,但是我似乎无法理解.

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.

数据框:

以人数为单位的系列结构:

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

我得到的是什么

我想要得到的是类似的东西,但是x值为婚姻状况:

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

Python和Matplotlib相当新,因此我对答案是否显而易见或问题措辞不佳表示敬意.

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

推荐答案

这应该有所帮助:

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

这篇关于Matplotlib条形图显示男性和女性的x值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆