0

I would like to add the y values to my plot for each year to make the graph easily readable but not sure how to do it. I have tried using the enumerate function but it does not return the desired output. Any guidance on this would be helpful.

import numpy as np
import pandas as pd


from matplotlib import pyplot
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

laikipia = pd.DataFrame({
    "2020":[5.21, 20.91, 17.05],
    "2021":[20.91, 19.91, 19.76],
    }, 
    index=["Cropland", "Forestland", "Shrubland"]
)
c = ['#FFDB5C', '#358221', '#EECFA8']

laikipia.plot(kind="bar", color=c)
plt.title("")
plt.xlabel("Laikipia LULC")
plt.ylabel("Area Loss (ha)")
plt.legend(laikipia, bbox_to_anchor=(1, 1))
plt.xticks(rotation=0)
#plt.yticks(round [plotdata], 0)

x = laikipia

y = [70, 60, 50, 40, 30, 20, 10, 0]

max_y_lim = max(y) 
min_y_lim = min(y)
plt.ylim(min_y_lim, max_y_lim)


for i, v in enumerate(y):
    plt.text(0, i, y[i], str(v), ha="center", va = "bottom") 
         
plt.show()
plt.tight_layout()

plot output

Peter Leimbigler
  • 10,775
  • 1
  • 23
  • 37
Shiraz
  • 3
  • 4
  • use [`bar_label`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bar_label.html): `ax = laikipia.plot.bar(color=c); ax.bar_label(ax.containers[0])` – tdy Nov 24 '21 at 14:55
  • 1
    @tdy thanks a lot it works perfectly! – Shiraz Nov 24 '21 at 16:08

0 Answers0