AttributeError: ‘AxesSubplot‘ object has no attribute ‘bar_label‘

AttributeError: ‘AxesSubplot’ object has no attribute ‘bar_label’

目录

AttributeError: ‘AxesSubplot’ object has no attribute ‘bar_label’

问题:

解决:

完整错误:

问题:

想为seaborn可视化的分组条形图添加数值标签信息;

使用bar_label函数添加标签,但是,没有找到bar_label函数;发生错误;

import matplotlib as mpl
import seaborn as sns

new_n = [0.17,0.25,0.67,0.97]
old_n = [0.1,0.19,0.38,0.55]

labels = ['X','X','X','X','Y','Y','Y','Y']
data_count = {'group':labels,'values':old_n+new_n,'way':[0.5,1,2,3,0.5,1,2,3]}

df = pd.DataFrame(data_count)

df

import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']

chart = sns.barplot(y='values',x='group',hue='way',data=df);

chart.bar_label(ax.containers[0])
for p in chart.patches:
             chart.annotate(p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
                 ha='center', va='center', fontsize=10, color='black', xytext=(0, 5),
                 textcoords='offset points')
plt.show()

解决:

AttributeError: ‘AxesSubplot’ object has no attribute ‘bar_label’

没有找到bar_label函数,有学者建议更新matplotlib,笔者更新后,仍然无效,使用如下方法;

import matplotlib as mpl
import seaborn as sns

new_n = [97,25,67,97]
old_n = [1,49,28,155]

labels = ['X','X','X','X','Y','Y','Y','Y']
data_count = {'group':labels,'values':old_n+new_n,'way':[0.5,1,2,3,0.5,1,2,3]}

df = pd.DataFrame(data_count)

df

import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']

chart.bar_label(ax.containers[0])

chart = sns.barplot(y='values',x='group',hue='way',data=df);

chart.bar_label(ax.containers[0])
for p in chart.patches:
             chart.annotate(p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
                 ha='center', va='center', fontsize=10, color='black', xytext=(0, 5),
                 textcoords='offset points')
plt.show()

AttributeError: ‘AxesSubplot‘ object has no attribute ‘bar_label‘

完整错误:

Original: https://blog.csdn.net/zhongkeyuanchongqing/article/details/125266330
Author: Data+Science+Insight
Title: AttributeError: ‘AxesSubplot‘ object has no attribute ‘bar_label‘

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/777551/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球