【问题标题】:Error for bar chart in matplotlib python 3matplotlib python 3中的条形图错误
【发布时间】:2021-08-15 04:35:10
【问题描述】:

我正在尝试使用 matplotlib 制作一个包含误差线(来自标准偏差值)的条形图(来自平均值),但一直遇到错误提示:

'Rectangle' object has no property 'labels'

如果我删除这一行:

'Rectangle' object has no property 'rotation'

对正在发生的事情进行一些帮助和澄清会很棒!以下是我目前所拥有的示例:

list1=[1,2,3,4,5,6]
list2=[0.1,0.2,0.3,0.4,0.5,0.6]
list3=['a','b','c','d','e','f']
x = range(len(list1))
y = list1 #contains mean values in list
z = list2 #contains std values in list
l=list3 #contains label names
plt.bar(x, y, yerr=z, color='green', rotation='vertical', label=l)  
plt.show()

谢谢!

【问题讨论】:

  • plt.bar 不接受旋转参数,所以你应该把它拿出来让你的代码工作。就标签而言,如果您在显示绘图之前调用 plt.legend() 方法,它用于为绘图图例中的所有绿色条提供一个名称。你想达到什么目的?可能有更好的方法。

标签: python-3.x matplotlib bar-chart bioinformatics


【解决方案1】:

IIUC,

list1=[1,2,3,4,5,6]
list2=[0.1,0.2,0.3,0.4,0.5,0.6]
list3=['a','b','c','d','e','f']
x = range(len(list1))
y = list1 #contains mean values in list
z = list2 #contains std values in list
l=list3 #contains label names
rects = plt.bar(x, y, yerr=z, color='green', label=l)
ax = plt.gca()
ax.bar_label(rects)
plt.show()

输出:


list1=[1,2,3,4,5,6]
list2=[0.1,0.2,0.3,0.4,0.5,0.6]
list3=['a','b','c','d','e','f']
x = range(len(list1))
y = list1 #contains mean values in list
z = list2 #contains std values in list
l=list3 #contains label names
rects = plt.barh(x, y, xerr=z, color='green', label=l)
ax = plt.gca()
ax.bar_label(rects)
plt.show()

输出:

【讨论】:

  • 如何在栏的顶部显示错误值?
  • @0Knowledge 将此行 ax.bar_label(rects, list2) 更改为带有 list2 的标签。
【解决方案2】:

错误是由于plt.bar()不知道关键字rotationlabel导致的,因此这些参数被传递给较低级别​​的绘图函数,但显然这些函数也无法处理它们。

要绘制水平条形图,您可以使用plt.barh()。请注意,您随后希望将错误值作为xerr 而不是yerr 传递。标签的关键字是tick_label

plt.barh(x, y, xerr=z, color='green', tick_label=l)

【讨论】:

    猜你喜欢
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2011-01-11
    相关资源
    最近更新 更多