给定四边形的角,使用 matplotlib 在 3d 中绘制阴影四边形

Given corners of a quadrilateral, Plot shaded quadrilateral in 3d using matplotlib

我有一个矩形的 4 个角(像这样)。

all_corners[0] = [[16.9491164719, 17.8960237441, 0.0],
    [17.89665059755603, 27.22995253575736, 0.0],
    [16.9491164719, 17.8960237441, 2.552],
    [17.89665059755603, 27.22995253575736, 2.552]]

我需要在 3d 中绘制此矩形 space 并且 我还需要以类似的方式绘制多个矩形。有什么办法可以做到这一点?我还需要它来处理任意形状,如四边形、多边形,但现在它是次要的,首先我需要绘制一个具有给定角坐标的阴影矩形。

我尝试了 Poly3dCollection,我得到了奇怪的三角形,而不是矩形,而且我发现很多方法不是我想要的,或者我遇到了错误。

from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)  
ax.add_collection3d(Poly3DCollection(all_corners[0]))
ax.set_ylim3d(10,40)
ax.set_xlim3d(10,40)
ax.set_zlim3d(0,4)
plt.show()

你的代码差不多好了。你只有两个小错误:

  1. 顶点按照提供的顺序连接。您应该按所需的顺序传递它们。否则,您将得到一个 self-intersecting 多边形。
  2. Poly3DCollection 需要一个类似数组的对象列表,其中 每个 项目描述一个多边形。如果您想一次添加多个多边形,这很有用。因此,你不应该传递 all_corners[0] 而是 all_corners 作为第一个参数
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt

all_corners = []

all_corners.append([[16.9491164719, 17.8960237441, 0.0],
    [17.89665059755603, 27.22995253575736, 0.0],
    [17.89665059755603, 27.22995253575736, 2.552],  # note the permutation of
    [16.9491164719, 17.8960237441, 2.552],          # these two points
    ])

# You can add many polygons at once
all_corners.append([[10, 10, 0.0], [15, 15, 0.0], [15, 15, 2],
    [10, 10, 2]])

fig = plt.figure()
ax = Axes3D(fig)  
ax.add_collection3d(Poly3DCollection(all_corners)) # list of array-like
ax.set_ylim3d(10,40)
ax.set_xlim3d(10,40)
ax.set_zlim3d(0,4)
plt.show()