带有来自数据框的条形图的 matplotlib

matplotlib with bar chart from dataframe

我曾尝试将此图形更改为条形图,但我做不到。我哪里错了?

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

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

参考的数据框是:

  Ethnicity          TOT Population  ID Race ID Ethnicity  ID Year       HPO
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

我想要一个条形图: x 轴为 TOT 人口,y 轴为年份,柱形代表西班牙裔人口而非西班牙裔人口

第一个 pivot your data and than use barh 水平条形图:

df.pivot('ID Year', 'Ethnicity', 'TOT Population').plot.barh()

由于西班牙裔人口比非西班牙裔人口少几个数量级,您需要对轴进行对数刻度以查看西班牙裔人口的条形:

import pandas as pd

df = pd.DataFrame({'Ethnicity': ['Hispanic or Latino', 'Hispanic or Latino', 'Hispanic or Latino', 'Hispanic or Latino', 'Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino', 'Not Hispanic or Latino'],
                   'TOT Population': [9825, 12234, 12437, 13502, 14025, 14616636, 14725729, 14815122, 14849129, 14884539],
                   'ID Race': [4, 4, 4, 4, 4, 0, 0, 0, 0, 0],
                   'ID Ethnicity': [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                   'ID Year': [2013, 2014, 2018, 2016, 2019, 2017, 2016, 2015, 2014, 2013],
                   'HPO': [2345, 2660, 2429, 3254, 2644, 7788, 8629, 7888, 7495, 6586]})

df.pivot('ID Year', 'Ethnicity', 'TOT Population').plot.barh(logx=True)