简体   繁体   English

如何在 matplotlib 中收集 plot 和 3D 补丁集?

[英]How to plot a 3D patch collection in matplotlib?

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.我正在尝试在 matplotlib 中制作 3D plot ,上面有三个圆,每个圆都以原点为中心,半径为 1,指向不同方向的球体。

In 2D I would make a circle patch collection and add it to the axes.在 2D 中,我会制作一个圆形补丁集合并将其添加到轴上。 In 3D I'm having trouble getting the patches to show up at all, let alone orient them in different directions.在 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()

Running this program fills the entire plot window with blue, ie the face color of the patches, no matter how I rotate the plot.运行这个程序会用蓝色填充整个 plot window,即补丁的面颜色,无论我如何旋转 plot。 If I set facecolor='none' in the PatchCollection() call, then an empty Axes3D shows up.如果我在PatchCollection()调用中设置facecolor='none' ,则会出现一个空的Axes3D

Things I've tried:我尝试过的事情:

  • If I use a CircleCollection instead of a PatchCollection , no patches show up at all.如果我使用CircleCollection而不是PatchCollection ,则根本不会显示任何补丁。
  • The zs parameter in the patch_collection_2d_to_3d() call is odd; patch_collection_2d_to_3d()调用中的zs参数是奇数; 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:我希望放置zs=0 (所有三个补丁的一个 z 坐标)或zs=[0,0,0] (每个补丁的一个单独的 z 坐标),但这两个都会引发错误:

    ValueError: setting an array element with a sequence. ValueError:使用序列设置数组元素。

  • 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'] .为了以不同的方式定位补丁,我希望能够传递类似zdir=['x', 'y', 'z']但无论我通过它还是'z'['z']的结果都没有什么不同['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:我还希望能够执行ax.add_collection3d(circles, zs=[0, 0, 0], zdir=['x', 'y', 'z'])而不是从 2d 转换补丁集合到 3d,但这也会引发错误:

    AttributeError: 'Patch3DCollection' object has no attribute 'set_sort_zpos' AttributeError: 'Patch3DCollection' object 没有属性 'set_sort_zpos'

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM