weixin_46773566 2021-04-01 23:30 采纳率: 50%
浏览 2394
已结题

Matplotlib中pyplot.barlabel()不能使用

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

N = 5
menMeans = (20, 35, 30, 35, -27)
womenMeans = (25, 32, 34, 20, -25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

fig, ax = plt.subplots()
p1=ax.bar(ind,menMeans,width,yerr=menStd,label='men',align='edge')
p2=ax.bar(ind,womenMeans,width,label='women',align='edge',bottom=menMeans,yerr=womenStd)
ax.axhline(0,color='grey',linewidth=0.8)
ax.set_title('Scores by group and gender')
ax.set_ylabel("Scores")
ax.legend()
ax.set_xticks([0,1,2,3,4])
ax.set_xticklabels(('G1','G2','G3','G4','G5'))

# Label with label_type 'center' instead of the default 'edge'
ax.bar_label(p1, label_type='center')
ax.bar_label(p2, label_type='center')
ax.bar_label(p2)

plt.show()

最后面几行代码ax.bar_label()报错

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

而在Matplotlib官网中有ax.bar_label()的使用方式

  • 写回答

4条回答 默认 最新

  • 四度水 2022-02-07 16:51
    关注

    matplotlib版本低了,升级就好

    本回答被专家选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 9月4日
  • 专家已采纳回答 8月27日