[英]Creating 3D Surface Plot with matplotlib in Python
with the following Code I'm able to create a 3D surface plot like this . 用下面的代码我能够创建一个三维表面的情节是这样 。 But i'd rather have it like this.
但是我宁愿这样。 interpolated plot
插值图
What do i need to interpolate it to a real surface without this single points? 没有这些单点我需要将其插值到真实表面吗?
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()
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. 您可以使用
scipy
在2D曲面上进行插值。 Here , in the first answer, he mentioned three different interpolation method, interp2d/splines, griddata and Rbf. 在这里 ,他在第一个答案中提到了三种不同的插值方法:interp2d /样条线,griddata和Rbf。 You can search them on google and read the
scipy
manual for more details. 您可以在Google上搜索它们,并阅读
scipy
手册以了解更多详细信息。
PS 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. PS我必须说我在您的代码中更改了
Y
,因为在手册中 ,它说x
和y
对于非矩形网格应具有相同的长度。 So check your code carefully with the dimension of x
, y
and z
to produce the result you want. 因此,请仔细检查代码中
x
, y
和z
的大小,以产生所需的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.