1

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

1 Answer 1

2

First pivot your data and than use barh for a horizontal bar chart:

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

As the Hispanic population is several orders of magnitude smaller than the non Hispanic, you'll need to log scale the axis to see the bars for the Hispanic population:

import matplotlib.pyplot as plt
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]})
ax= df.pivot('ID Year', 'Ethnicity', 'TOT Population').plot.barh(logx=True)
plt.xlabel('TOT Population')
plt.ylabel('Year')
plt.title('My plot')
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.