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一样为不同值设置不同颜色
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