从Groupby的特定数据帧上的Matplotlib图形
我想拥有一个带有X轴的图形,用于tot的人口,Y轴是多年的,而西班牙裔则有两行,而不是西班牙裔。数据框架是:
ID Race ID Ethnicity ID Year Hispanic Population Moe
Ethnicity TOT Population
Hispanic or Latino 9825 4.0 1.0 2013.0 2345.0
12234 4.0 1.0 2014.0 2660.0
12437 4.0 1.0 2018.0 2429.0
13502 4.0 1.0 2016.0 3254.0
14025 4.0 1.0 2019.0 2644.0
... ... ... ... ... ...
Not Hispanic or Latino
14616636 0.0 0.0 2017.0 7788.0
14725729 0.0 0.0 2016.0 8629.0
14815122 0.0 0.0 2015.0 7888.0
14849129 0.0 0.0 2014.0 7495.0
14884539 0.0 0.0 2013.0 6586.0
我从民族和poplation的组中获得了此数据框。有人可以帮助我做一个真正的matplotlib吗?谢谢你!
i want to have a graphic with x axis for TOT Population, y axis for Years and two lines one for Hispanic and one for not Hispanic. The dataframe is that:
ID Race ID Ethnicity ID Year Hispanic Population Moe
Ethnicity TOT Population
Hispanic or Latino 9825 4.0 1.0 2013.0 2345.0
12234 4.0 1.0 2014.0 2660.0
12437 4.0 1.0 2018.0 2429.0
13502 4.0 1.0 2016.0 3254.0
14025 4.0 1.0 2019.0 2644.0
... ... ... ... ... ...
Not Hispanic or Latino
14616636 0.0 0.0 2017.0 7788.0
14725729 0.0 0.0 2016.0 8629.0
14815122 0.0 0.0 2015.0 7888.0
14849129 0.0 0.0 2014.0 7495.0
14884539 0.0 0.0 2013.0 6586.0
I got this dataframe from a groupby of Ethnicity and TOT Poplation. SOmeone can help me to make real a matplotlib? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的问题有两个部分。首先是将分组的数据移至
maatplotlib
可以理解(基本上使表格)的格式,其次是绘制一个图中的两行。初始数据:
首先,请使用reset_index弄平表,
然后绘制线图。
您的图形看起来像这样。您可以根据需要进一步更改它。请注意,与非西班牙裔人口相比,西班牙裔人口很少。因此,该图的范围很宽。您只能绘制一组,看到起伏更好。
输出图
I believe there are two parts to your question. First is to move the grouped data to a format that
maatplotlib
would understand (basically flatten the table) and second to plot (lines) the two lines in one graph.The initial data:
First, use reset_index to flatten the table
You then plot the line graph.
Your graph will look like this. You can change it further as you need. Note that the Hispanic population is rather small compared to the non-hispanic population. So, the graph was made rather wide. You can plot just one group and see the ups and downs better.
Output graph