在matplotlib/scipy/numpy中打印日志数组时出错

2024-10-01 10:13:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  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.

代码很简单:

^{pr2}$

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


Tags: inpyselfmatplotlibeggpackageslinelibrary
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()

所以也许问题出在你没有给我们看的东西上。你能提供一个完整的示例代码片段吗?这样我们就可以重现你的问题了吗?在

我最近也遇到了同样的问题:

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

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

array = numpy.float64(array)

希望这有帮助:~)

新答案:

从源代码看,如果传入仿射_变换的点数组维数错误或为空,则会引发此错误。以下是相关线路:

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或-inf(在等于0的情况下),这当然无法绘制。在

(不过,惠索是对的-这些观点被忽略了)

相关问题 更多 >