0

this is my code with the output:

plt.figure(figsize = (7,4))
df1.groupby(["Area"])["Crop_Value(hg/ha)"].sum().sort_values(ascending = False).nlargest(5).plot(kind = "bar")
plt.title("Top 5 Countries with most crop production")
plt.show()

enter image description here

I am trying to visualize the top 5 countries with most crop production. The values in Y-axis are not useful like the way they are.

How do I change it to actual Crop Production Values? OR
How can I display the values on top of each bar?

Thanks

Yilmaz
  • 117
  • 2
  • 9
  • Does this answer your question? https://stackoverflow.com/questions/30228069/how-to-display-the-value-of-the-bar-on-each-bar-with-pyplot-barh – Ananda Jan 29 '21 at 04:34
  • I tried that already. But it's not working with my code... – Yilmaz Jan 29 '21 at 04:38
  • Then you need to post that code along with a more details as to why it did not work. Please keep in mind that when posting on SO, you need to show what you have tried, what didn't work for you and any error you got as a result. Take a look at the guidelines. https://stackoverflow.com/help/how-to-ask – Ananda Jan 29 '21 at 04:41

1 Answers1

1

You need to use a different formatter than the standard ScalarFormatter, e.g. a FormatStrFormatter:

import matplotlib.pyplot as plt

plt.subplots()
plt.bar(['India', 'Brazil'], [3e8,2e8])
plt.gca().yaxis.set_major_formatter(plt.matplotlib.ticker.FormatStrFormatter('%.1e'))

enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52