1

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.

1
  • 2
    you need to specify more information. For example look at myarray_x, myarray_y before calling plt.scatter Commented Apr 22, 2010 at 18:47

3 Answers 3

7

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 :~)

1
  • 1
    In fact this is the correct answer I think.At least it solved my problem. Commented May 25, 2012 at 9:36
5

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)

3
  • Ding ding ding.... that sounds like what "ValueError: Invalid vertices array." is trying to say. Commented Apr 22, 2010 at 18:54
  • 2
    @Jusint: No. These values are ignored when plotting Commented Apr 22, 2010 at 18:59
  • @Justin, that would make sense, but that is not what pyplot does; at least under normal circumstances, pyplot just ignores such values. For example, if I run by attempt to reproduce with my_array = array([[1.0, 2.0], [3.0, 4.0], [-5.0, 0.0]]) instead, it does not issue this error for me, but rather plot a graph with only two points. Commented Apr 22, 2010 at 19:07
4

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.