AxesSubplot' object has no attribute 'datavalues'

Hello everyone:
I want to generate a bar plot based on my data by using ‘bar_label’, But I always get the error ‘AxesSubplot’ object has no attribute ‘datavalues’. I’m using the version of matplotlib “3.5.3”. If anyone knows how to solve my problem, please tell me how to do. Thank you very much!
Here is my code:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.ticker import FuncFormatter

matplotlib.rcParams['font.sans-serif']=['SimHei']
matplotlib.rcParams['axes.unicode_minus']=False

filename = 'cagr_data.xlsx'
df = pd.read_excel(filename, index_col=0, sheet_name='Sheet2')

fig, ax = plt.subplots(figsize=(10,5), dpi=100)
rects = df.plot.bar(ax=ax, legend=True, fontsize=15)

def percentage(x, pos):
    return '{:.0%}'.format(x)

ax.yaxis.set_major_formatter(FuncFormatter(percentage))
ax.set_xticklabels(ax.get_xticklabels(), rotation=0, ha='center')
ax.set_xlabel('')
ax.set_ylabel('')
ax.bar_label(rects, label_type='edge')

Thanks again.

bar_label is supposed to be used on the container returned from Matplotlib’s bar.

You are using Panda’s bar, which returns a (list of) Axes, not the container.

Understood. Thank you so much!