简体   繁体   English

scipy.interpolate 中的 bisplrep 和 bisplev 问题

[英]Problems with bisplrep and bisplev from scipy.interpolate

I receive some quite strange behavior with bisplrep and bisplev from the scipy.interpolate module.我收到来自scipy.interpolate模块的bisplrepbisplev的一些非常奇怪的行为。 I try to reproduce the example from the scipy homepage ( https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html ). 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 .我有一些 function f(x,y)计算我一些 z 值,然后我使用bisplrep进行插值。 If I recalculate the data using bisplev and plot the data the resulting values appear rotated about 90°.如果我使用bisplev和 plot 重新计算数据,则结果值显示旋转了大约 90°。 Even if I exchange the x and y values the plot is still rotated.即使我交换 x 和 y 值 plot 仍然旋转。 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.我正在使用最新版本的 scipy,错误发生在 Jupyter、Spyder 和 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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