Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a bar chart based on the presented data frame?

Data Frame

I am trying to produce a bar chart for the data frame above and I am having issues doing so. My code for this data frame is:

high_pay[['Sport', 'Name', 'Year', 'earnings ($ million)']] \
    .sort_values('earnings ($ million)', ascending=False) \
    .drop_duplicates('Sport', keep='first')

What would be the appropriate way to put this information in a bar chart where Sport is on the x axis and earnings ($ million) is on the y axis?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes you can do that with something like the following.

import matplotlib.pyplot as plt

high_pay = pd.DataFrame({'Sport': ['Boxing','Soccer','Golf','Tennis','MMA','NFL','NBA','Auto Racing','Baseball','Hockey','Cycling','American Football / Baseball'],
                   'Earnings ($ million)': [300,127,115,106.3,99,89.5,89,80,35,29.8,28,22.5]
                   })


plt.bar(high_pay["Sport"].values, high_pay['Earnings ($ million)'].values)
#format labels on x-axis
plt.xticks(rotation=45, ha='right')
plt.show()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading