I am trying to add value labels to a plot with matplotlib using bar_label. My data is from a DataFrame. I am getting the error AttributeError: 'AxesSubplot' object has no attribute 'datavalues' . I tried looking at different answers to similar problems here in StackOverflow and elsewhere, but I still don't understand how to fix this. Could someone point me in the right direction, please?
My version of matplotlib is 3.6.0, so that is not the issue.
If I try building it from a list, such as the example below, it works fine and it generates the plot with the value labels that I want.
year = [1999, 2000, 2001]
animals = [40, 50, 10]
barplot = plot.bar(year,
animals,
fc = "lightgray",
ec = "black")
plt.bar_label(container = barplot, labels = y, label_type = "center")
plt.show()
The problem is when I try to get the values from a DataFrame. For example, the code below:
year_v2 = [1999, 2010, 2011]
animals_v2 = [400, 500, 100]
df_v2 = pd.DataFrame([year_v2, animals_v2], index = ["year", "animals"]).T
barplot_v2 = df_v2.plot.bar("year",
"animals",
fc = "lightgray",
ec = "black")
plt.bar_label(container = barplot_v2,
labels = "animals",
label_type = "center")
plt.show()
The plot is generated, but without the value labels and with the error AttributeError: 'AxesSubplot' object has no attribute 'datavalues'.

