The pylab scatter plot appears to be transposed

I play with different interpolation methods - and especially love the varieties shown in the YouTube video https://www.youtube.com/watch?v=_cJLVhdj0j4

However, the scatter module displays the points in the wrong place. I moved them below (example 5) to get it working, but that doesn't work unless the area of โ€‹โ€‹interest is focused on the origin (Test_Rbf).

Am I misunderstanding 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)

      

+3


source to share


1 answer


If you are using

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

      

instead

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

      



This xnew

will change as you move through the columns, and ynew

will change as you move through the rows. (I changed the x-range from [-1.1] to [-2.2] to make it clear which numbers control the range).

Combine that with @ hitzg's suggestion to add origin='lower'

to the call 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

+1


source







All Articles