【问题标题】:Creating 3D Surface Plot with matplotlib in Python在 Python 中使用 matplotlib 创建 3D 曲面图
【发布时间】:2017-03-15 18:01:08
【问题描述】:

使用以下代码,我可以创建 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()

【问题讨论】:

    标签: python matplotlib 3d surface


    【解决方案1】:
    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()
    

    我只修改了你的部分代码,得到如下图。

    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)
    

    您可以使用scipy 在二维曲面上进行插值。 Here,在第一个答案中,他提到了三种不同的插值方法,interp2d/splines、griddata 和 Rbf。您可以在 google 上搜索它们并阅读 scipy manual 了解更多详细信息。

    附:我不得不说我在您的代码中更改了Y,因为在manual 中,它说xy 对于非矩形网格应该具有相同的长度。所以仔细检查你的代码,用xyz的维度来产生你想要的结果。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2015-09-13
      • 2016-10-11
      相关资源
      最近更新 更多