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

我有一个矩形的 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 空间中绘制这个矩形,我还需要以类似的方式绘制多个矩形。有什么办法可以做到这一点?我还需要它来处理任意形状,如四边形、多边形,但现在它是次要的,首先我需要绘制一个具有给定角坐标的阴影矩形。

我尝试了 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. 顶点按照提供的顺序连接。您应该按所需的顺序传递它们。否则,您将得到一个自相交的多边形。
  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()

https://stackoverflow.com/questions/70864778/

相关文章:

javascript - 在 React.js 中使用 Dropzone 时如何将图像上传到 Fir

c++ - 用方括号 [] 初始化 std::vector ;怎么了?

python - Kubernetespodoperator如何使用cmds或者cmds和argum

python - 可视化 DASK 任务图

exception - 如何跳过在 Kafka 中产生运行时异常的记录并保持流运行?

visual-studio - 如何在 D 盘中完全安装 Visual Studio?

r - 计算矩阵内的元素

java - Lombok 继承 : how to set default value for fi

python - PyTorch 中的 DataLoader 和 DataLoader2 有什么不同

ios - FBSDKGraphRequest 实例方法 '-startWithCompletion