【问题标题】:matplotlib.pyplot.errorbar is throwing an error it shouldn't?matplotlib.pyplot.errorbar 抛出不应该的错误?
【发布时间】:2016-08-10 05:24:26
【问题描述】:

我正在尝试使用我的数据制作误差线图。 X 是一个 9 元素的 ndarray。 Y 和 Yerr 是 9x5 ndarrays。当我打电话时:

matplotlib.pyplot.errorbar(X, Y, Yerr)

我得到一个 ValueError:“yerr 必须是一个标量,与 y 的尺寸相同,或者 2xN。”

但是Y.shape == Yerr.shape 是真的。

我在 64 位 Windows 7 上运行 Spyder 2.3.8 和 Python 3.5.1。 Matplotlib 是最新的。我已经为 Visual Studio 2015 安装了 Visual C++ Redistributable。

有什么想法吗?

编辑:一些数据。

X=numpy.array([1,2,3])
Y=numpy.array([[1,5,2],[3,6,4],[9,3,7]])
Yerr=numpy.ones_like(Y)

【问题讨论】:

  • 包含一些生成触发此问题的示例数据的代码
  • 嗯...错误消息与文档字符串不符,即*xerr*/*yerr*: [ scalar | N, Nx1, or 2xN array-like ]

标签: python python-3.x matplotlib errorbar


【解决方案1】:

也许文档中的“y 维度”实际上是指 1xN...

无论如何,这可以工作:

for y, yerr in zip(Y, Yerr):
    matplotlib.pyplot.errorbar(X, y, yerr)

【讨论】:

    【解决方案2】:

    嗯....

    通过研究引发我们发现的错误的模块的第 2962-2965 行

    if len(yerr) > 1 and not ((len(yerr) == len(y) and not (iterable(yerr[0]) and len(yerr[0]) > 1)))
    

    来自数据

    1 T len(yerr) > 1
    2 T len(yerr) == len(y)
    3 T iterable(yerr[0])
    4 T len(yerr[0]) > 1
    5 T 1 and not (2 and not (3 and 4)
    

    但是,如果以下测试未通过,则不会触发:

    if (iterable(yerr) and len(yerr) == 2 and
                    iterable(yerr[0]) and iterable(yerr[1])):
    ....
    

    并且它没有被触发,因为 len(yerr) = 3

    除了维度之外,一切似乎都检查过了。这有效:

    X = numpy.tile([1,2,3],3)
    Y = numpy.array([1,5,2,3,6,4,9,3,7])
    Yerr = numpy.ones_like(Y)
    

    我不确定是什么导致了错误。 "l0, = " 赋值似乎也有点古怪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 2011-01-05
      • 2014-09-21
      • 2023-01-31
      • 2021-06-02
      相关资源
      最近更新 更多