【问题标题】:"Invalid RGBA argument" error when specifying list of colours for errorbar为错误栏指定颜色列表时出现“无效的 RGBA 参数”错误
【发布时间】:2021-04-08 12:58:00
【问题描述】:

我希望每个点都有不同的颜色误差条,如下所示:

我正在尝试这段代码:

ax.errorbar([450.,500.], [4,5] , xerr = ([20.,15.],[20.,15.]), fmt ='o',color=['r','b'], ecolor = ['r','b'],ms=4, capsize=4,)

但我收到以下错误。我做错了什么?

  238         # float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
    239         # Test dimensionality to reject single floats.
--> 240         raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
    241     # Return a tuple to prevent the cached value from being modified.
    242     c = tuple(c.astype(float))

ValueError: Invalid RGBA argument: ['r', 'b']

【问题讨论】:

  • 你打算通过将错误栏的“颜色”属性设置为['r', 'b']来实现什么?
  • 我希望每个点的误差条具有不同的颜色。如果我做 color = 'blue' 它工作正常。
  • 我已经添加了我想要的东西的图像

标签: python matplotlib errorbar


【解决方案1】:

在我看来,您必须将它们一个一个添加到情节中

import numpy as np
x = np.array([450., 500., 550., 600.])
y = np.array([4, 5, 4.5, 6])
xerr = np.array([[20.,15.],[20.,15.], [10.,25.], [12.,18.]])
color = 'rbyg'
for i in range(len(x)):
    plt.gca().errorbar(x[i], y[i], xerr=xerr[i].reshape(2, -1),
                        fmt ='o',
                        color=color[i%len(color)],
                        ecolor=color[i%len(color)],
                        ms=4,
                        capsize=4)
plt.show()

【讨论】:

  • 如果点数多于颜色,i%len(color) 部分会循环重复颜色。
  • 太好了,如果您能将问题设置为已回答就好了:) 谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-08-18
  • 2018-07-05
  • 2020-06-30
  • 2012-08-28
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多