Home > OS >  Error using bar_label to insert value labels on plot from dataframe, on Python with pandas and matpl
Error using bar_label to insert value labels on plot from dataframe, on Python with pandas and matpl

Time:01-12

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'.

CodePudding user response:

You need to access the containers in the axes with using bar_label from the axessubplot:

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")

barplot_v2.bar_label(container = barplot_v2.containers[0],
              labels = animals_v2,
              label_type = "center")

plt.show()

Output:

enter image description here

CodePudding user response:

Suggestion:

You can try matplotlib.plyplot.text() method for adding value labels to your chart. The method has the following parameters.

matplotlib.plyplot.text(x, y, s, ha, Bbox)

x,y = coordinates of the plot
s = String to be displayed
ha = Horizontal alignment
Bbox = the rectangular box around the text

Sample code:

import matplotlib.pyplot as plt
import pandas as pd

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")

def valuelabel(x,y):
    for i in range(len(x)):
        plt.text(i,y[i],y[i], ha = 'center')
        
valuelabel(df_v2.year,df_v2.animals)

plt.show()

Output:

enter image description here

References:

https://www.geeksforgeeks.org/adding-value-labels-on-a-matplotlib-bar-chart/ https://pythonguides.com/matplotlib-bar-chart-labels/

  • Related