matplotlib 打印曲面,就像三维框一样

q35jwt9p  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(143)

我会使用这些边界点和分数点创建一个曲面盒。当我想用ax绘制曲面时,我感到困惑。plot_surface(),这是我的代码:

import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt

    points = [[0, 0, 0], 
              [12.28, 0, 0],
              [12.28, 8.94, 0], 
              [0, 8.94, 0], 
              [0, 0, 0.81], 
              [12.28, 0, 0],
              [12.28, 8.94, 0.81], 
              [0, 8.94, 0]]

   fig = plt.figure()
   ax = fig.add_subplot(111, projection='3d')
   ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])
   ax.set_xlabel('X')
   ax.set_ylabel('Y')
   ax.set_zlabel('Z')
   plt.show()

先谢谢你了

umuewwlo

umuewwlo1#

要使用.plot_surface()绘制曲面,需要先创建meshgrid,为其指定z值,然后进行绘制。下面的代码演示了如何绘制立方体。

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

x_data = [0, 10]
y_data = [0, 10]

xModel = np.linspace(min(x_data), max(x_data), 10)
yModel = np.linspace(min(y_data), max(y_data), 10)
X, Y = np.meshgrid(xModel, yModel)

def func2(data, m):
    return m*np.ones([len(data[0]), len(data[1])])

points = np.array([[0, 0, 0], 
          [0, 10, 0],
          [10, 0, 0], 
          [10, 10, 0], 
          [0, 0, 10], 
          [0, 10, 10],
          [10, 0, 10], 
          [10, 10, 10]])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

alpha = 0.35
ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])

Z1 = func2(np.array([X, Y]), 0)
Z2 = func2(np.array([X, Y]), 10)

# There are 6 sides of the cube to plot
# Bottom
ax.plot_surface(X, Y, Z1, rstride=1, cstride=1, cmap="coolwarm", \
                linewidth=1, antialiased=True, alpha=alpha)

# Top. Not plotted intentionally.
#ax.plot_surface(X, Y, Z2, rstride=1, cstride=1, cmap="coolwarm", \
#                linewidth=1, antialiased=True, alpha=alpha)

# Sides
ax.plot_surface(Z1, X, Y, rstride=1, cstride=1, cmap="gray_r", \
                linewidth=1, antialiased=True, alpha=alpha)
ax.plot_surface(Z2, X, Y, rstride=1, cstride=1, cmap="gray_r", \
                linewidth=1, antialiased=True, alpha=alpha)

ax.plot_surface(Y, Z1, X, rstride=1, cstride=1, cmap="copper_r", \
                linewidth=1, antialiased=True, alpha=alpha)
ax.plot_surface(Y, Z2, X, rstride=1, cstride=1, cmap="copper_r", \
                linewidth=1, antialiased=True, alpha=alpha)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

相关问题