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-un开发者_开发百科iversal.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.
The solution in this case was to recast the arrays to a lower precision float i.e.
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. 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.
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.
(wiso is right though - these points are just ignored)
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?
精彩评论