Home > OS >  matplotlib with bar chart from dataframe
matplotlib with bar chart from dataframe

Time:06-04

i've tried to change this graphic to a bar graph, but i couldn't. Where i'm going wrong?

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

enter image description here

The dataframe of reference is that:

  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

and i want a bar graph with: x axis with TOT Population, y axis Years, and bar for hispanic population and not hispanic pop

CodePudding user response:

First enter image description here

  • Related