使用以下代码我可以创建3D曲面图like this。但我宁愿这样做。 interpolated plot
如果没有这一点,我需要将它插入真实表面?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
array1 = [3, 551, 536, 528, 551, 424, 547, 549, 504, 551, 425, 424, 611, 528, 583, 551, 532, 551, 543, 543, 549, 456, 551, 551, 543, 551, 551, 547, 671, 549, 1064, 1247, 1155, 1304, 870, 583, 1064, 871, 1064, 1064, 871, 1064, 2151, 3107, 3135, 2695, 1191, 1119, 1064, 1127, 1064, 1064, 1088, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 1158, 1635, 2311, 2590, 3048, 2627, 2231, 1960, 1576, 1080, 1064, 615, 1064, 615, 677, 613, 551, 611, 743, 549, 1064, 1064, 1064, 1848, 2920, 5276, 7895, 9128, 8534, 6112, 3636, 2311, 1576, 1064, 1064, 1064, 1064, 679, 1064, 1064, 1159, 1576, 2072, 2309, 2824, 2824, 2563, 2562, 2114, 1760, 1848, 1576, 1576, 1832, 2307, 4080, 6088, 6552, 6494, 5096, 3263, 2563, 2151, 2143, 1976, 1884, 1623, 1189, 1112, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 743, 1064, 675, 1064, 1064, 639, 1064, 1064, 549, 936, 551, 583, 1064, 611, 1064, 613, 613, 1064, 583, 615, 1064, 551, 871, 551, 740, 1064, 1064, 1064, 1064, 871, 1064, 871, 583, 551, 547, 532, 551, 440, 551, 551, 542, 583, 549, 547, 551, 472, 551, 547, 520, 543, 536, 534, 1064, 549, 743, 679, 675, 1064, 551, 551, 551, 543, 743, 546, 543, 647, 504, 551, 677, 543, 583, 551, 528, 583, 551, 548, 583, 543, 674, 551, 551, 551, 551, 646, 674, 551, 1064, 936, 1064, 1064, 551, 611, 871, 550, 871, 613, 549, 740, 547, 550, 743, 549, 678, 613, 551, 615, 551, 551, 743, 551, 551, 551, 528, 1064, 551, 547, 677, 520, 547, 549, 551, 740, 544, 549, 607, 512, 675, 528, 512, 551, 528, 528, 549, 540, 551, 551, 551, 679, 532, 549, 551, 528, 607, 544, 543, 583, 1296]
A = 321.1035679
B = 2.690772395
C = -9.715588933E-4
D = -9.601752918E-6
E = 1.787982949E-8
F = -8.642571168E-12
i=1
waves=[]
while i<=288:
waves.append(round(A+B*i+C*i**2+D*i**3+E*i**4+F*i**5))
i=i+1
# Make data.
X = waves[2:288]
Z = array1
Y = np.arange(1, 50, 1)
X, Y = np.meshgrid(X, Y)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
#ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
答案 0 :(得分:0)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
array1 = [3, 551, 536, 528, 551, 424, 547, 549, 504, 551, 425, 424, 611, 528, 583, 551, 532, 551, 543, 543, 549, 456, 551, 551, 543, 551, 551, 547, 671, 549, 1064, 1247, 1155, 1304, 870, 583, 1064, 871, 1064, 1064, 871, 1064, 2151, 3107, 3135, 2695, 1191, 1119, 1064, 1127, 1064, 1064, 1088, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 1158, 1635, 2311, 2590, 3048, 2627, 2231, 1960, 1576, 1080, 1064, 615, 1064, 615, 677, 613, 551, 611, 743, 549, 1064, 1064, 1064, 1848, 2920, 5276, 7895, 9128, 8534, 6112, 3636, 2311, 1576, 1064, 1064, 1064, 1064, 679, 1064, 1064, 1159, 1576, 2072, 2309, 2824, 2824, 2563, 2562, 2114, 1760, 1848, 1576, 1576, 1832, 2307, 4080, 6088, 6552, 6494, 5096, 3263, 2563, 2151, 2143, 1976, 1884, 1623, 1189, 1112, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 1064, 743, 1064, 675, 1064, 1064, 639, 1064, 1064, 549, 936, 551, 583, 1064, 611, 1064, 613, 613, 1064, 583, 615, 1064, 551, 871, 551, 740, 1064, 1064, 1064, 1064, 871, 1064, 871, 583, 551, 547, 532, 551, 440, 551, 551, 542, 583, 549, 547, 551, 472, 551, 547, 520, 543, 536, 534, 1064, 549, 743, 679, 675, 1064, 551, 551, 551, 543, 743, 546, 543, 647, 504, 551, 677, 543, 583, 551, 528, 583, 551, 548, 583, 543, 674, 551, 551, 551, 551, 646, 674, 551, 1064, 936, 1064, 1064, 551, 611, 871, 550, 871, 613, 549, 740, 547, 550, 743, 549, 678, 613, 551, 615, 551, 551, 743, 551, 551, 551, 528, 1064, 551, 547, 677, 520, 547, 549, 551, 740, 544, 549, 607, 512, 675, 528, 512, 551, 528, 528, 549, 540, 551, 551, 551, 679, 532, 549, 551, 528, 607, 544, 543, 583, 1296]
A = 321.1035679
B = 2.690772395
C = -9.715588933E-4
D = -9.601752918E-6
E = 1.787982949E-8
F = -8.642571168E-12
i=1
waves=[]
while i<=288:
waves.append(round(A+B*i+C*i**2+D*i**3+E*i**4+F*i**5))
i=i+1
# Make data.
X = waves[2:288]
Z = array1
Y = np.arange(1, 287, 1)
f = si.interp2d(np.array(X), np.array(Y), Z, kind='cubic')
X_smooth=np.linspace(np.array(X).min(),np.array(X).max(),100)
Y_smooth=np.linspace(np.array(Y).min(),np.array(Y).max(),100)
Z_smooth=f(X_smooth,Y_smooth)
X_smooth, Y_smooth = np.meshgrid(X_smooth, Y_smooth)
# Plot the surface.
surf = ax.plot_surface(X_smooth, Y_smooth, Z_smooth, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
#ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
I only changed part of your code and get the following picture.
Y = np.arange(1, 287, 1)
f = si.interp2d(np.array(X), np.array(Y), Z, kind='cubic')
X_smooth=np.linspace(np.array(X).min(),np.array(X).max(),100)
Y_smooth=np.linspace(np.array(Y).min(),np.array(Y).max(),100)
Z_smooth=f(X_smooth,Y_smooth)
X_smooth, Y_smooth = np.meshgrid(X_smooth, Y_smooth)
# Plot the surface.
surf = ax.plot_surface(X_smooth, Y_smooth, Z_smooth, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
You can use scipy
to do the interpolation on a 2D surface. Here, in the first answer, he mentioned three different interpolation method, interp2d/splines, griddata and Rbf. You can search them on google and read the scipy
manual for more details.
P.S. I have to say that I changed Y
in your code, because in the manual, it says x
and y
should have same length for non rectangular grid. So check your code carefully with the dimension of x
, y
and z
to produce the result you want.