在 python 中绘制插值曲面
plot interpolated surface in python
我有一个 4x4 矩阵 (test4x4
),我想将它插值到一个 8x8 矩阵 (test8x8
)。我使用 interpolate.interp2d
进行插值,但是当我绘制它时 (test8x8
),它看起来不像 test4x4
绘图。我哪里弄错了?
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
mymin,mymax = 0,3
X = np.linspace(mymin,mymax,4)
Y = np.linspace(mymin,mymax,4)
x,y = np.meshgrid(X,Y)
test4x4 = np.array([[ 1.2514318 , 1.25145821, 1.25148472, 1.25151133],
[ 1.25087456, 1.25090105, 1.25092764, 1.25095435],
[ 1.25031581, 1.25034238, 1.25036907, 1.25039586],
[ 1.24975557, 1.24978222, 1.24980898, 1.24983587]])
f = interpolate.interp2d(x,y,test4x4,kind='cubic')
# use linspace so your new range also goes from 0 to 3, with 8 intervals
Xnew = np.linspace(mymin,mymax,8)
Ynew = np.linspace(mymin,mymax,8)
test8x8 = f(Xnew,Ynew)
print('test8x8=',test8x8)
plot1=plt.figure(1)
plt.title('test 4X4')
fig1 = plt.figure(1)
ax1 = fig1.gca(projection='3d')
ax1.plot_surface(x.T,y.T, test4x4, alpha = 1, rstride=1, cstride=1, linewidth=0.5, antialiased=True, zorder = 0.5)
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
ax1.plot_surface(Xnew.T, Ynew.T, test8x8, alpha = 1, rstride=1, cstride=1, linewidth=0.5, antialiased=True, zorder = 0.5)
plt.grid()
plt.show()
我想我可以解决这个问题,我应该使用x1,y1 = np.meshgrid(Xnew,Ynew)
。
对于 8x8,您还需要一个网格:
...
Xnew = np.linspace(mymin,mymax,8)
Ynew = np.linspace(mymin,mymax,8)
xx, yy = np.meshgrid(Xnew, Ynew) #You need this
并使用这个网格来绘制
ax1.plot_surface(xx.T, yy.T, test8x8, alpha=0.5, rstride=1, cstride=1, \
linewidth=0.5, antialiased=True, zorder = 10)
在两个 plot_surface()
中使用 alpha=0.5
,这样您就可以看到两个表面。
为了更清楚地分开两个表面,您可以尝试将第二个 .plot_surface()
设为
ax1.plot_surface(xx.T, yy.T, 0.0001+test8x8, alpha=0.5, rstride=1, cstride=1, \
linewidth=0.5, antialiased=True, zorder = 10)
值 0.0001 使第二个表面更高(z-direction)。
我有一个 4x4 矩阵 (test4x4
),我想将它插值到一个 8x8 矩阵 (test8x8
)。我使用 interpolate.interp2d
进行插值,但是当我绘制它时 (test8x8
),它看起来不像 test4x4
绘图。我哪里弄错了?
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
mymin,mymax = 0,3
X = np.linspace(mymin,mymax,4)
Y = np.linspace(mymin,mymax,4)
x,y = np.meshgrid(X,Y)
test4x4 = np.array([[ 1.2514318 , 1.25145821, 1.25148472, 1.25151133],
[ 1.25087456, 1.25090105, 1.25092764, 1.25095435],
[ 1.25031581, 1.25034238, 1.25036907, 1.25039586],
[ 1.24975557, 1.24978222, 1.24980898, 1.24983587]])
f = interpolate.interp2d(x,y,test4x4,kind='cubic')
# use linspace so your new range also goes from 0 to 3, with 8 intervals
Xnew = np.linspace(mymin,mymax,8)
Ynew = np.linspace(mymin,mymax,8)
test8x8 = f(Xnew,Ynew)
print('test8x8=',test8x8)
plot1=plt.figure(1)
plt.title('test 4X4')
fig1 = plt.figure(1)
ax1 = fig1.gca(projection='3d')
ax1.plot_surface(x.T,y.T, test4x4, alpha = 1, rstride=1, cstride=1, linewidth=0.5, antialiased=True, zorder = 0.5)
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
ax1.plot_surface(Xnew.T, Ynew.T, test8x8, alpha = 1, rstride=1, cstride=1, linewidth=0.5, antialiased=True, zorder = 0.5)
plt.grid()
plt.show()
我想我可以解决这个问题,我应该使用x1,y1 = np.meshgrid(Xnew,Ynew)
。
对于 8x8,您还需要一个网格:
...
Xnew = np.linspace(mymin,mymax,8)
Ynew = np.linspace(mymin,mymax,8)
xx, yy = np.meshgrid(Xnew, Ynew) #You need this
并使用这个网格来绘制
ax1.plot_surface(xx.T, yy.T, test8x8, alpha=0.5, rstride=1, cstride=1, \
linewidth=0.5, antialiased=True, zorder = 10)
在两个 plot_surface()
中使用 alpha=0.5
,这样您就可以看到两个表面。
为了更清楚地分开两个表面,您可以尝试将第二个 .plot_surface()
设为
ax1.plot_surface(xx.T, yy.T, 0.0001+test8x8, alpha=0.5, rstride=1, cstride=1, \
linewidth=0.5, antialiased=True, zorder = 10)
值 0.0001 使第二个表面更高(z-direction)。