从Groupby的特定数据帧上的Matplotlib图形

发布于 2025-02-04 08:04:42 字数 1168 浏览 1 评论 0原文

我想拥有一个带有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆那么伤 2025-02-11 08:04:42

我相信您的问题有两个部分。首先是将分组的数据移至maatplotlib可以理解(基本上使表格)的格式,其次是绘制一个图中的两行。

初始数据:

>> df
                                     ID Race    ID Ethnicity    ID Year  Hispanic...
Ethnicity          TOT Population               
Hispanic or Latino           9825       4               1        2013       2345
                             12234      4               1        2014       2660
                             12437      4               1        2018       2429
                             13502      4               1        2016       3254
                             14025      4               1        2019       2644
Not Hispanic or Latino      14616636    0               0        2017       7788
                            14725729    0               0        2016       8629
                            14815122    0               0        2015       7888
                            14849129    0               0        2014       7495
                            14884539    0               0        2013       6586

首先,请使用reset_index弄平表,

>> df2 = df.reset_index()
>> df2
   Ethnicity    TOT Population  ID Race ID Ethnicity    ID Year Hispanic Population Moe
0   Hispanic or Latino  9825    4   1   2013    2345
1   Hispanic or Latino  12234   4   1   2014    2660
2   Hispanic or Latino  12437   4   1   2018    2429
3   Hispanic or Latino  13502   4   1   2016    3254
4   Hispanic or Latino  14025   4   1   2019    2644
5   Not Hispanic or Latino  14616636    0   0   2017    7788
6   Not Hispanic or Latino  14725729    0   0   2016    8629
7   Not Hispanic or Latino  14815122    0   0   2015    7888
8   Not Hispanic or Latino  14849129    0   0   2014    7495
9   Not Hispanic or Latino  14884539    0   0   2013    6586

然后绘制线图。

import matplotlib.pyplot as plt
plt.figure(figsize=(20, 5))

plt.plot(df2['TOT Population'][df2['Ethnicity'] == 'Hispanic or Latino'], df2['ID Year'][df2['Ethnicity'] == 'Hispanic or Latino'])
plt.plot(df2['TOT Population'][df2['Ethnicity'] == 'Not Hispanic or Latino'], df2['ID Year'][df2['Ethnicity'] == 'Not Hispanic or Latino'], '-.')
plt.ticklabel_format(style='plain')
plt.xlabel("TOT Population")
plt.ylabel("Year")
plt.title('My plot')

您的图形看起来像这样。您可以根据需要进一步更改它。请注意,与非西班牙裔人口相比,西班牙裔人口很少。因此,该图的范围很宽。您只能绘制一组,看到起伏更好。

输出图

”输出图

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:

>> df
                                     ID Race    ID Ethnicity    ID Year  Hispanic...
Ethnicity          TOT Population               
Hispanic or Latino           9825       4               1        2013       2345
                             12234      4               1        2014       2660
                             12437      4               1        2018       2429
                             13502      4               1        2016       3254
                             14025      4               1        2019       2644
Not Hispanic or Latino      14616636    0               0        2017       7788
                            14725729    0               0        2016       8629
                            14815122    0               0        2015       7888
                            14849129    0               0        2014       7495
                            14884539    0               0        2013       6586

First, use reset_index to flatten the table

>> df2 = df.reset_index()
>> df2
   Ethnicity    TOT Population  ID Race ID Ethnicity    ID Year Hispanic Population Moe
0   Hispanic or Latino  9825    4   1   2013    2345
1   Hispanic or Latino  12234   4   1   2014    2660
2   Hispanic or Latino  12437   4   1   2018    2429
3   Hispanic or Latino  13502   4   1   2016    3254
4   Hispanic or Latino  14025   4   1   2019    2644
5   Not Hispanic or Latino  14616636    0   0   2017    7788
6   Not Hispanic or Latino  14725729    0   0   2016    8629
7   Not Hispanic or Latino  14815122    0   0   2015    7888
8   Not Hispanic or Latino  14849129    0   0   2014    7495
9   Not Hispanic or Latino  14884539    0   0   2013    6586

You then plot the line graph.

import matplotlib.pyplot as plt
plt.figure(figsize=(20, 5))

plt.plot(df2['TOT Population'][df2['Ethnicity'] == 'Hispanic or Latino'], df2['ID Year'][df2['Ethnicity'] == 'Hispanic or Latino'])
plt.plot(df2['TOT Population'][df2['Ethnicity'] == 'Not Hispanic or Latino'], df2['ID Year'][df2['Ethnicity'] == 'Not Hispanic or Latino'], '-.')
plt.ticklabel_format(style='plain')
plt.xlabel("TOT Population")
plt.ylabel("Year")
plt.title('My plot')

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

Output Graph

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文