pylab scatter plot appears transposed

454 views Asked by At

I have been playing with various interpolation techniques - and particularly like the varieties shown in the youtube video https://www.youtube.com/watch?v=_cJLVhdj0j4

However, the scatter module plots the points in the wrong location. I have transposed them below (Example 5) to make it work, but this does not work if the area of interest is not centred on the origin (Test_Rbf).

Am I mis-understanding something fundamental, or is this a problem in the pylab scatter module?

# Example 5 
#
# https://www.youtube.com/watch?v=_cJLVhdj0j4

import numpy as np
from scipy import interpolate
import pylab as py

def func(x,y):
return (x+y)*np.cos(-5.0*x + 4.0*y)

x = np.random.uniform(-1.0, 1.0,size=50)
y = np.random.uniform(-1.0, 1.0,size=50)
fvals = func(x,y)

newfunc = interpolate.Rbf(x, y, fvals, function='multiquadric')
xnew, ynew = np.mgrid[-1:1:100j, -1:1:100j]

fnew = newfunc(xnew, ynew)
true = func(xnew, ynew)

py.figure()
py.clf()
py.imshow( fnew, extent=[-1,1,-1,1], cmap=py.cm.jet)
# py.scatter( x, y, 30, fvals, cmap=py.cm.jet)
py.scatter( y, -x, 30, fvals, cmap=py.cm.jet)

py.show()

from enthought.mayavi import mlab

mlab.clf()
mlab.surf(xnew, ynew, fnew*2)
1

There are 1 answers

1
unutbu On

If you use

ynew, xnew = np.mgrid[-1:1:100j, -2:2:100j]

instead of

xnew, ynew = np.mgrid[-1:1:100j, -2:2:100j]

then xnew will vary as you move across columns, and ynew will vary as you move down the rows. (I changed the x-range from [-1,1] to [-2,2] to make it clear what numbers control which range.)

Combine that with @hitzg's suggestion to add origin='lower' to the call to imshow, and you get:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
np.random.seed(2015)
def func(x,y):
    return (x+y)*np.cos(-5.0*x + 4.0*y)

x = np.random.uniform(-2.0, 2.0, size=50)
y = np.random.uniform(-1.0, 1.0, size=50)
fvals = func(x,y)

newfunc = interpolate.Rbf(x, y, fvals, function='multiquadric')
ynew, xnew = np.mgrid[-1:1:100j, -2:2:100j]

fnew = newfunc(xnew, ynew)

plt.figure()
plt.imshow(fnew, extent=[-2,2,-1,1], cmap=plt.cm.jet, origin='lower')
plt.scatter(x, y, s=30, c=fvals, cmap=plt.cm.jet)
plt.show()

enter image description here