如何在 matplotlib 中绘制 3D 补丁集合?
我正在尝试在 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 aPatchCollection
, no patches show up at all. The
zs
parameter in thepatch_collection_2d_to_3d()
call is odd; I would expect to put eitherzs=0
(one z-coordinate for all three patches) orzs=[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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)