简体   繁体   English

在matplotlib / scipy / numpy中绘制日志数组时出错

[英]error when plotting log'd array in matplotlib/scipy/numpy

I have two arrays and I take their logs. 我有两个数组,我记录了它们的日志。 When I do that and try to plot their scatter plot, I get this error: 当我这样做并尝试绘制其散点图时,出现此错误:

  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.

the code is simply: 代码很简单:

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

any idea what could be causing this? 知道是什么原因造成的吗? thanks. 谢谢。

I had the same problem which I fixed recently: 我遇到了最近解决的相同问题:

The problem for me was that my X and Y (numpy) arrays were made up of 128 bit floats. 对我来说,问题是我的X和Y(numpy)数组由128位浮点数组成。

The solution in this case was to recast the arrays to a lower precision float ie 在这种情况下,解决方案是将数组重铸为较低精度的浮点数,即

array = numpy.float64(array)

Hope this helps :~) 希望这会有所帮助:〜)

New Answer: 新答案:

From looking at the source, this error is thrown if the points array passed into affine_transform is of the wrong dimensions or if it is empty. 从源头看,如果传递给affine_transform的points数组的尺寸错误或为空,则会引发此错误。 Here are the relevant lines: 以下是相关行:

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.");

Take a look at the dimensions of your myarray_x and myarray_y arrays before going ahead maybe. 在继续进行之前,请看一下myarray_x和myarray_y数组的尺寸。

Old Answer : 旧答案

My best guess it that you are taking the log of values <= 0. This will either give you nan's or -infs(in the case of being equal to 0) in your arrays which of course it can't plot. 我最大的猜想是,您正在取值<= 0的对数。这将在数组中给出nan或-infs(在等于0的情况下),当然它无法绘制。

(wiso is right though - these points are just ignored) (wiso是正确的-这些要点将被忽略)

This runs successfully for me 这对我来说成功运行了

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

so perhaps the problem is with something you haven't shown us. 所以问题可能出在您未向我们展示的内容上。 Can you provide a complete piece of sample code exactly as you ran it so we can reproduce your problem? 您能否提供与运行时一样的完整示例代码,以便我们重现您的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM