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

在尝试为Seaborn的分组条形图添加数值标签时,遇到了`AxesSubplot`对象没有`bar_label`属性的错误。问题在于Seaborn库本身不提供`bar_label`函数,而该功能可能属于matplotlib的更新特性。通过使用matplotlib的`annotate`方法,手动为每个条形添加标签解决了问题。代码示例展示了如何遍历条形图的`patches`来实现这一功能。
摘要由CSDN通过智能技术生成

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                            Traceback (most recent call last)
<ipython-input-21-8d228d87356c> in <module>
     12 ax = sns.barplot(y='values',x='group',hue='口径',data=df);
     13 
---> 14 ax.bar_label(ax.containers[0])

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

数据可视化就是使用图形图表等方式来呈现数据,图形图表能够高效清晰地表达数据包含的信息。数据可视化在各个领域都得到了广泛的应用,例如,产品销售数据的可视化,统计样本数据可视化,机器学习数据可视化等。
Python有很多非常优秀易用的数据可视化的库,其中最著名的要数matplotlib了,它有着十几年的历史,至今仍然是Python使用者最常用的画图库。
Seaborn跟matplotlib最大的区别就是它的默认绘图风格和色彩搭配都具有现代美感,其实是在matplotlib的基础上进行了更高级的API封装,让你能用更少的代码去调用 matplotlib的方法,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图,而使用matplotlib就能制作具有更多特色的图。应该把Seaborn视为matplotlib的补充,而不是替代物。

一、matplotlib

官网:Matplotlib — Visualization with Python

数据可视化常用的库,直接看官网最好了,有丰富的例子并配有代码,而是最权威的

  • matplot是python的基础的可视化库,可以配置很多参数画出丰富的图形。

二、seaborn

        seaborn是在matplotlib基础上进一步封装,很多方法可以直接使用默认参数,用起来方便,比如底层自动配色,不需要像matplot一样为不同值设置不同颜色

官网:http://seaborn.pydata.org/

Matplotlib

It is an amazing visualization library in Python for 2D plots of arrays, It is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002. Let’s try to understand some of the benefits and features of matplotlib

  • It’s fast, efficient as it is based on numpy and also easier to build
  • Has undergone a lot of improvements from the open source community since inception and hence a better library having advanced features as well
  • Well maintained visualization output with high quality graphics draws a lot of users to it
  • Basic as well as advanced charts could be very easily built
  • From the users/developers point of view, since it has a large community support, resolving issues and debugging becomes much easier

Seaborn

Conceptualized and built originally at the Stanford University, this library sits on top of matplotlib. In a sense, it has some flavors of matplotlib while from the visualization point, its is much better than matplotlib and has added features as well. Below are its advantages

  • Built-in themes aid better visualization
  • Statistical functions aiding better data insights
  • Better aesthetics and built-in plots
  • Helpful documentation with effective examples

参考:seaborn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data+Science+Insight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值