在matplotlib / scipy / numpy中绘制log'd数组时出错

时间:2010-04-22 18:26:58

标签: python numpy matplotlib scipy

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

  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)

任何可能导致这种情况的想法?感谢。

3 个答案:

答案 0 :(得分:7)

我遇到了最近修复过的问题:

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

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

array = numpy.float64(array)

希望这会有所帮助:〜)

答案 1 :(得分:5)

新答案:

从查看源代码时,如果传入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数组的尺寸。

旧答案

我最好猜测你正在记录值的对数< = 0.这会在你的数组中给你nan或-infs(在等于0的情况下)当然它无法绘制

(虽然这是正确的 - 这些点只是被忽略了)

答案 2 :(得分:4)

这为我成功运行

>>> 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()

所以也许问题在于你没有向我们展示的东西。您能否像运行它一样提供完整的示例代码,以便我们重现您的问题?