Question

I found a strange behavior of Python, numpy, or matplotlib.

Please execute following code with pylab on ipython:

>>> import numpy as np
>>> import numpy.random as nr
>>> import matplotlib.pyplot as mp
>>> data = nr.rand(50, 2)
>>> mp.scatter(data[:, 0], data[:, 1])
>>> trans = np.asmatrix(data.T)
>>> mp.scatter(trans[0, :], trans[1, :])
>>> all(data == trans.T)

I expected two figures created by this code are exactly same, but they look slightly different. The last line of the code also implies they are exactly same.

What is the problem?

python 2.7.4, IPython 0.13.2, pylab 1.7.1, numpy 1.7.1, matplotlib 1.2.0

Was it helpful?

Solution

The np.asmatrix() just convert an iterable to a matrix, when applicable. So:

 trans = np.asmatrix(data.T)
 np.all( data == trans.T )

should always give True

The problem with the different plots is that in matplotlib.axes.Axes.scatter the ravel() is executed in the numpy.ma (Masked Arrays) module. Here, despite data==trans.T, np.ma.ravel(trans[0,:]) returns a matrix instead of a flattened array. To fix that you can call np.ravel(), that works for non-masked arrays. I opened this issue in GitHub to report this, which is possibly a bug...

The result will be:

fig, axs = mp.subplots(nrows=1, ncols=2, sharey=True )

axs[0].scatter(data[:, 0], data[:, 1])

trans = np.asmatrix(data.T)

axs[1].scatter( np.ravel(trans[0,:]), np.ravel(trans[1,:]) )

fig.tight_layout()

giving you this:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top