-4

I have a dataframe and it has few columns. I want to plot the top 10 countries with highest life expectancy rate. Here is my dataset enter image description here

It should be in matplotlib or seaborn.

Malik Hamza
  • 181
  • 1
  • 10

1 Answers1

1

Use DataFrame.nlargest with DataFrame.plot.bar:

(df.nlargest(10, 'Life expectancy(years)(Country)')
   .plot.bar(x='City', y='Life expectancy(years)(Country)'))

For seaborn use seaborn.barplot:

import seaborn as sns
sns.barplot(x='City', 
            y='Life expectancy(years)(Country)',
            data=df.nlargest(10, 'Life expectancy(years)(Country)'))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • It worked. Thanks a lot. How can we increase the size of graph or plot in sns.barplot – Malik Hamza Mar 03 '22 at 09:52
  • @MalikHamza - yes, you can do it, check [this](https://stackoverflow.com/questions/51174691/how-to-increase-image-size-of-pandas-dataframe-plot) – jezrael Mar 03 '22 at 10:04
  • @MalikHamza - and for seaborn use [this](https://stackoverflow.com/questions/31594549/how-to-change-the-figure-size-of-a-seaborn-axes-or-figure-level-plot) – jezrael Mar 03 '22 at 10:05