【问题标题】:error when plotting log'd array in matplotlib/scipy/numpy在 matplotlib/scipy/numpy 中绘制日志数组时出错
【发布时间】:2011-02-11 05:05:43
【问题描述】:

我有两个数组,我记录了它们的日志。当我这样做并尝试绘制他们的散点图时,我得到了这个错误:

  File "/Library/Python/2.6/site-packages/matplotlib-1.0.svn_r7892-py2.6-macosx-10.6-universal.egg/matplotlib/pyplot.py", line 2192, in scatter
    ret = ax.scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, faceted, verts, **kwargs)
  File "/Library/Python/2.6/site-packages/matplotlib-1.0.svn_r7892-py2.6-macosx-10.6-universal.egg/matplotlib/axes.py", line 5384, in scatter
    self.add_collection(collection)
  File "/Library/Python/2.6/site-packages/matplotlib-1.0.svn_r7892-py2.6-macosx-10.6-universal.egg/matplotlib/axes.py", line 1391, in add_collection
    self.update_datalim(collection.get_datalim(self.transData))
  File "/Library/Python/2.6/site-packages/matplotlib-1.0.svn_r7892-py2.6-macosx-10.6-universal.egg/matplotlib/collections.py", line 153, in get_datalim
    offsets = transOffset.transform_non_affine(offsets)
  File "/Library/Python/2.6/site-packages/matplotlib-1.0.svn_r7892-py2.6-macosx-10.6-universal.egg/matplotlib/transforms.py", line 1924, in transform_non_affine
    self._a.transform(points))
 File "/Library/Python/2.6/site-packages/matplotlib-1.0.svn_r7892-py2.6-macosx-10.6-universal.egg/matplotlib/transforms.py", line 1420, in transform
    return affine_transform(points, mtx)
ValueError: Invalid vertices array.

代码很简单:

myarray_x = log(my_array[:, 0])
myarray_y = log(my_array[:, 1])
plt.scatter(myarray_x, myarray_y)

知道是什么原因造成的吗?谢谢。

【问题讨论】:

  • 您需要指定更多信息。例如在调用plt.scatter之前查看myarray_xmyarray_y

标签: python numpy matplotlib scipy


【解决方案1】:

我最近解决了同样的问题:

我的问题是我的 X 和 Y (numpy) 数组是由 128 位浮点数组成的。

这种情况下的解决方案是将数组重铸为精度较低的浮点数,即

array = numpy.float64(array)

希望这会有所帮助:~)

【讨论】:

  • 其实这是我认为的正确答案。至少它解决了我的问题。
【解决方案2】:

新答案:

通过查看源代码,如果传递给 affine_transform 的点数组的维度错误或为空,则会引发此错误。以下是相关行:

if (!vertices ||
        (PyArray_NDIM(vertices) == 2 && PyArray_DIM(vertices, 1) != 2) ||
        (PyArray_NDIM(vertices) == 1 && PyArray_DIM(vertices, 0) != 2))
         throw Py::ValueError("Invalid vertices array.");

在继续之前看看你的 myarray_x 和 myarray_y 数组的维度。

旧答案

我最好的猜测是您正在记录值

(不过,wiso 是对的——这些点被忽略了)

【讨论】:

  • 叮叮叮......这听起来像“ValueError:无效的顶点数组”。正想说。
  • @Jusint: 不。这些值在绘图时会被忽略
  • @Justin,这是有道理的,但这不是 pyplot 所做的;至少在正常情况下,pyplot 只是忽略这些值。例如,如果我尝试使用my_array = array([[1.0, 2.0], [3.0, 4.0], [-5.0, 0.0]]) 进行复制,它不会为我发出此错误,而是绘制一个只有两个点的图表。
【解决方案3】:

这对我来说运行成功

>>> from numpy import log, array
>>> import matplotlib.pyplot as plt
>>> my_array = array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
>>> my_array
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
>>> myarray_x = log(my_array[:, 0])
>>> myarray_y = log(my_array[:, 1])
>>> plt.scatter(myarray_x, myarray_y)
<matplotlib.collections.CircleCollection object at 0x030C7A10>
>>> plt.show()

所以问题可能出在您没有向我们展示的东西上。您能否提供一段与您运行时完全相同的完整示例代码,以便我们重现您的问题?

【讨论】:

    猜你喜欢
    • 2016-10-18
    • 2021-06-16
    • 1970-01-01
    • 2021-08-22
    • 2021-04-30
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多