如何在 matplotlib 中绘制 3D 补丁集合?

发布于 2024-11-14 20:25:57 字数 1425 浏览 4 评论 0原文

我正在尝试在 matplotlib 中制作一个 3D 绘图,上面有三个圆圈,每个圆圈以原点为中心,半径为 1,指向不同的方向 - 例如,以说明半径为 1 的球体。

在 2D 中,我会制作一个圆形补丁集合并将其添加到轴上。在 3D 中,我根本无法让补丁显示出来,更不用说将它们定向到不同的方向了。

import matplotlib
import matplotlib.pyplot as P
import mpl_toolkits.mplot3d as M3

fig = P.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
circles = matplotlib.collections.PatchCollection(
    [matplotlib.patches.Circle((0, 0), 1) for count in range(3)],
    offsets=(0, 0))
M3.art3d.patch_collection_2d_to_3d(circles, zs=[0], zdir='z')
ax.add_collection(circles)
P.show()

无论我如何旋转绘图,运行该程序都会用蓝色(即色块的表面颜色)填充整个绘图窗口。如果我在 PatchCollection() 调用中设置 facecolor='none',则会显示一个空的 Axes3D

我尝试过的事情:

  • 如果我使用 CircleCollection 而不是 PatchCollection,则根本不会显示任何补丁。
  • patch_collection_2d_to_3d() 调用中的 zs 参数是奇数;我希望放置 zs=0 (所有三个补丁的一个 z 坐标)或 zs=[0,0,0] (一个单独的 z 坐标每个补丁),但这两个都会抛出错误:

    <块引用>

    ValueError:使用序列设置数组元素。

  • 为了以不同的方式定位补丁,我希望能够传递类似 zdir=['x', 'y', 'z'] 的内容,但无论我是否通过,结果都没有什么不同或 'z'['z']

  • 我还希望能够执行 ax.add_collection3d(circles, zs=[0, 0, 0], zdir=['x', 'y', 'z'])将补丁集合从 2d 转换为 3d,但这也会引发错误: <块引用>

    AttributeError:'Patch3DCollection'对象没有属性'set_sort_zpos'

I'm trying to make a 3D plot in matplotlib with three circles on it, each centered at the origin and with a radius of 1, pointing in different directions - to illustrate a sphere of radius 1, for example.

In 2D I would make a circle patch collection and add it to the axes. In 3D I'm having trouble getting the patches to show up at all, let alone orient them in different directions.

import matplotlib
import matplotlib.pyplot as P
import mpl_toolkits.mplot3d as M3

fig = P.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
circles = matplotlib.collections.PatchCollection(
    [matplotlib.patches.Circle((0, 0), 1) for count in range(3)],
    offsets=(0, 0))
M3.art3d.patch_collection_2d_to_3d(circles, zs=[0], zdir='z')
ax.add_collection(circles)
P.show()

Running this program fills the entire plot window with blue, i.e. the face color of the patches, no matter how I rotate the plot. If I set facecolor='none' in the PatchCollection() call, then an empty Axes3D shows up.

Things I've tried:

  • If I use a CircleCollection instead of a PatchCollection, no patches show up at all.
  • The zs parameter in the patch_collection_2d_to_3d() call is odd; I would expect to put either zs=0 (one z-coordinate for all three patches) or zs=[0,0,0] (a separate z-coordinate for each patch), but both of those throw an error:

    ValueError: setting an array element with a sequence.

  • To orient the patches differently, I would expect to be able to pass something like zdir=['x', 'y', 'z'] but the results are no different whether I pass that or 'z' or ['z'].

  • I would also have expected to be able to do ax.add_collection3d(circles, zs=[0, 0, 0], zdir=['x', 'y', 'z']) instead of converting the patch collection from 2d to 3d, but that throws an error too:

    AttributeError: 'Patch3DCollection' object has no attribute 'set_sort_zpos'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2024-11-21 20:25:57
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, PathPatch
from mpl_toolkits.mplot3d import Axes3D 
import mpl_toolkits.mplot3d.art3d as art3d


fig = plt.figure()
ax=fig.gca(projection='3d')

for i in ["x","y","z"]:
    circle = Circle((0, 0), 1)
    ax.add_patch(circle)
    art3d.pathpatch_2d_to_3d(circle, z=0, zdir=i)


ax.set_xlim3d(-2, 2)
ax.set_ylim3d(-2, 2)
ax.set_zlim3d(-2, 2)

plt.show()
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, PathPatch
from mpl_toolkits.mplot3d import Axes3D 
import mpl_toolkits.mplot3d.art3d as art3d


fig = plt.figure()
ax=fig.gca(projection='3d')

for i in ["x","y","z"]:
    circle = Circle((0, 0), 1)
    ax.add_patch(circle)
    art3d.pathpatch_2d_to_3d(circle, z=0, zdir=i)


ax.set_xlim3d(-2, 2)
ax.set_ylim3d(-2, 2)
ax.set_zlim3d(-2, 2)

plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文