gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:36:18 24 4
gpt4 key购买 nike

我有一个矩形的 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()

关于python - 给定四边形的角,使用 matplotlib 在 3d 中绘制阴影四边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70864778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com