ptomato
ptomato

Reputation: 57850

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.

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:

Upvotes: 10

Views: 15220

Answers (1)

Lilith
Lilith

Reputation: 196

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

Upvotes: 18

Related Questions