說明:
是以在插值前,加兩句把輸入參數變成一維數組
XX = np.arange(0, 16, 1)
YY= np.arange(0, 12, 1)
代碼:
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from scipy import interpolate
import matplotlib as mpl
import matplotlib.cm as cm
fig = plt.figure()
Z=np.empty([12,16])
#ax = Axes3D(fig)
ax = fig.add_subplot(121, projection='3d')
bx = fig.add_subplot(122, projection='3d')
adc=[
29.38, 29.28, 29.23, 29.35, 29.53, 29.49, 29.41, 29.34, 29.42, 29.46, 29.58, 29.55, 29.49, 29.35, 29.46, 30.07,
29.43, 29.36, 29.39, 29.34, 29.55, 29.54, 29.44, 29.44, 29.60, 29.43, 29.73, 29.65, 29.46, 29.67, 29.87, 29.63,
29.45, 29.39, 29.33, 29.53, 29.59, 29.62, 29.44, 29.44, 29.62, 29.74, 29.60, 29.60, 29.54, 29.52, 29.63, 29.54,
29.51, 29.48, 29.59, 29.52, 29.63, 29.52, 29.58, 29.66, 29.68, 29.80, 29.59, 29.55, 29.53, 29.54, 29.74, 29.68,
29.48, 29.61, 29.62, 29.50, 29.57, 29.61, 29.63, 29.64, 30.01, 30.82, 29.86, 29.80, 29.74, 29.71, 29.80, 29.76,
29.79, 29.73, 29.53, 29.51, 29.66, 29.71, 29.69, 29.78, 29.97, 32.57, 30.31, 30.06, 29.84, 29.72, 29.53, 29.60,
29.64, 29.62, 29.59, 29.66, 29.67, 29.74, 29.74, 29.75, 29.78, 30.92, 31.55, 29.95, 29.94, 29.87, 29.76, 29.69,
29.70, 29.69, 29.78, 29.74, 29.88, 29.86, 29.81, 29.77, 29.76, 29.88, 31.88, 30.18, 29.87, 29.89, 29.93, 29.60,
29.56, 29.72, 29.83, 29.98, 31.13, 31.41, 29.81, 29.83, 29.85, 29.80, 30.17, 30.06, 29.87, 29.86, 30.12, 29.81,
29.85, 29.81, 29.81, 29.91, 30.28, 32.26, 30.99, 29.79, 29.85, 29.68, 29.81, 29.93, 29.83, 29.97, 29.93, 29.98,
30.20, 29.72, 29.95, 29.83, 30.05, 30.26, 31.81, 30.70, 29.66, 29.74, 29.79, 29.91, 30.25, 29.91, 29.90, 30.12,
30.95, 30.80, 29.93, 29.80, 29.80, 29.89, 30.00, 30.88, 29.78, 29.87, 29.77, 29.88, 30.83, 30.00, 29.79, 30.06
]
X = np.arange(0, 16, 1)
Y = np.arange(0, 12, 1)
X, Y = np.meshgrid(X, Y)
for i in range(0,12):
for j in range(0,16):
Z[i][j]=adc[i*16+j]
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
ax.contour(X,Y, Z, zdir = 'z', offset = 21, cmap = plt.get_cmap('rainbow'))
ax.set_zlim(20, 40)
znew=np.empty([48,64])
XX = np.arange(0, 16, 1)
YY= np.arange(0, 12, 1)
#二維插值
newfunc = interpolate.interp2d(XX, YY, Z, kind='cubic')
# 計算100*100的網格上的插值
xnew = np.linspace(0,16,64)#x
ynew = np.linspace(0,12,48)#y
znew = newfunc(xnew, ynew)
Xnew, Ynew = np.meshgrid(xnew, ynew)
bx.plot_surface(Xnew, Ynew, znew, rstride=1, cstride=1, cmap='rainbow')
bx.contour(Xnew,Ynew, znew, zdir = 'z', offset = 0, cmap = plt.get_cmap('rainbow'))
bx.set_zlim(0, 40)
plt.show()
原文連結:https://blog.csdn.net/m0_37738150/article/details/106247829