简体   繁体   中英

Problems with bisplrep and bisplev from scipy.interpolate

I receive some quite strange behavior with bisplrep and bisplev from the scipy.interpolate module. I try to reproduce the example from the scipy homepage ( https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html ). I have some function f(x,y) which calculates me some z-values which I then interpolate using bisplrep . If I recalculate the data using bisplev and plot the data the resulting values appear rotated about 90°. Even if I exchange the x and y values the plot is still rotated. Can somebody tell me if I am doing something completely wrong here? The following code should be sufficient to reproduce the error. I am using the most recent version of scipy and the error occurs in Jupyter, Spyder and in IDLE.

import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate

def f(x, y):
    return x**2 + y**2

x, y = np.linspace(0, 5, 15), np.linspace(-2*np.pi, 2*np.pi, 15)
xx, yy = np.meshgrid(x, y)
zz = f(xx, yy)

tck = interpolate.bisplrep(xx, yy, zz)

plt.pcolor(xx, yy, zz)

x_new, y_new = np.linspace(0, 5, 100), np.linspace(-2*np.pi, 2*np.pi, 100)

z_new = interpolate.bisplev(x_new, y_new, tck)

plt.figure()    
plt.pcolor(x_new, y_new, z_new)

plt.figure()    
plt.pcolor(y_new, x_new, z_new)

plt.show()

I used another definition for the grid. Now, it should work:

import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate

def f(x, y):
    return x**2 + y**2

#x, y = np.linspace(0, 5, 15), np.linspace(-2*np.pi, 2*np.pi, 15)
xx, yy = np.mgrid[0:5:15j, -2*np.pi:2*np.pi:15j]
zz = f(xx, yy)

tck = interpolate.bisplrep(xx, yy, zz)

plt.pcolor(xx, yy, zz)

#x_new, y_new = np.linspace(0, 5, 100), np.linspace(-2*np.pi, 2*np.pi, 100)
xx_new, yy_new = np.mgrid[0:5:100j, -2*np.pi:2*np.pi:100j]

zz_new = interpolate.bisplev(xx_new[:,0], yy_new[0,:], tck)

plt.figure()    
plt.pcolor(xx_new, yy_new, zz_new)

plt.figure()    
plt.pcolor(yy_new, xx_new, zz_new)

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM