gpt4 book ai didi

python - Pyplot 3d 散点图 : points at the back overlap points at the front

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:44 24 4
gpt4 key购买 nike

我正在使用 matplotlib 准备 3d 图,并且我对多个数据集有一个非常奇怪的行为。我有两个数据集,基本上描述了 3d 中的两个壳:一个内壳和一个外壳。要在 3d 中绘制它们,我会这样做:

fig = plt.figure()

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

ax.scatter(outer_z[:n], outer_x[:n], outer_y[:n], c='black', marker='.', lw=0)
ax.scatter(inner_z[:n], inner_x[:n], inner_y[:n], c='red', marker='.', lw=0)

ax.set_xlabel("Z")
ax.set_ylabel("X")
ax.set_zlabel("Y")

ax.set_xlim([-5,5])
ax.set_ylim([5,-5])
ax.set_zlim([-5,5])

(轴的顺序只是为了透视目的)。然而,当我保存图形时,我没有得到两个壳:

enter image description here enter image description here

我将一层覆盖在另一层之上,显然在后面的点出现在前面。你可以在图片上看到,一些应该在内壳后面的外壳点被绘制在内壳前面。这真的很烦人,因为它不追求“plot in 3d”的目的。有没有人知道为什么会发生这种情况以及如何解决?非常感谢!

最佳答案

我知道这不是您问题的解决方案,但也许可以解释为什么会这样。

这与 Matplotlib 实际上没有 3D 引擎这一事实有关。 Mplot3D 获取你的点并将它们投影到 2D 图上的样子(对于每个对象),然后 Matplotlib 一次绘制每个对象; Matplotlib 是一个 2D 绘图框架,而 Mplot3D 是一种小技巧,无需为 Matplotlib 编写成熟的 3D 引擎即可获得一些 3D 功能。

这意味着您绘制不同图(在本例中为红点和黑点)的顺序很重要,如果您在红点之后绘制黑点,它们看起来会在红点之前,无论他们的位置如何。

让我用另一个例子来说明这一点。

theta   = np.linspace(0, 2*np.pi, 100, endpoint=True)

helix_x = np.cos(3*theta)
helix_y = np.sin(3*theta)
helix_z = theta

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

line_x = np.zeros(100)
line_y = np.zeros(100)

ax.plot(line_x, line_y, theta, lw='3', color='r')
ax.plot(helix_x, helix_y, helix_z, lw='2', color='k')

ax.set_xlabel("Z")
ax.set_ylabel("X")
ax.set_zlabel("Y")

ax.set_xlim([-1.5,1.5])
ax.set_ylim([-1.5,1.5])
ax.set_zlim([0,2*np.pi])

这给出:enter image description here

但是从顶 View 你可以看到这条线在螺旋内部:

enter image description here

但是,如果您交换绘制这些线的顺序:

ax.plot(line_x, line_y, theta, lw='3', color='r')
ax.plot(helix_x, helix_y, helix_z, lw='2', color='k')

然后您会看到在螺旋后绘制的线:

enter image description here

最终这意味着您必须手动确定哪些点位于其他点的前面。然后你可以使用 zorder参数来确定哪些对象将在其他对象之前。但是您必须为每个视角(角度、仰角)执行此操作。在这种情况下,您可能必须将内线分解为“infront_of_helix”和“behind_helix”部分,然后分别将它们绘制在螺旋线的前面和后面。

我希望有人能对此事进行更详细的阐述,因为我自己对这个话题很感兴趣。我知道 mplot3d 有一些基本方法来确保前面的点首先显示,我相信,当它使用着色算法时,但我不确定。

关于python - Pyplot 3d 散点图 : points at the back overlap points at the front,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33151163/

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