How to plot a 3D patch collection in matplotlib?

To plot a 3D patch collection in matplotlib, we can create patches (like circles) and position them on different 3D planes. The pathpatch_2d_to_3d() method converts 2D patches into 3D objects that can be displayed in a 3D plot.

Steps to Create a 3D Patch Collection

  • Set the figure size and adjust the padding between and around the subplots.
  • Create a new figure or activate an existing figure.
  • Get the current axes and set projection as 3d.
  • Iterate through coordinate directions and create circle patches using pathpatch_2d_to_3d() method to convert a PathPatch to a PathPatch3D object.
  • To display the figure, use show() method.

Example

Here's how to create circular patches positioned on different planes in 3D space ?

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import mpl_toolkits.mplot3d.art3d as art3d

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

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

colors = ['red', 'green', 'blue']
for i, direction in enumerate(["x", "y", "z"]):
    circle = Circle((0.5, 0.5), 0.2, color=colors[i], alpha=0.7)
    ax.add_patch(circle)
    art3d.pathpatch_2d_to_3d(circle, z=0, zdir=direction)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('3D Patch Collection')

plt.show()

How It Works

The code creates three circles, each positioned on a different plane:

  • zdir='x' - Places the circle on the YZ plane
  • zdir='y' - Places the circle on the XZ plane
  • zdir='z' - Places the circle on the XY plane

Creating Multiple Patches at Different Positions

You can create a more complex collection by varying positions and sizes ?

import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Rectangle
import mpl_toolkits.mplot3d.art3d as art3d
import numpy as np

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

# Create circles at different Z positions
z_positions = [0, 1, 2]
colors = ['red', 'green', 'blue']

for i, (z_pos, color) in enumerate(zip(z_positions, colors)):
    # Create circle
    circle = Circle((0.3 + i*0.2, 0.3 + i*0.1), 0.15, color=color, alpha=0.6)
    ax.add_patch(circle)
    art3d.pathpatch_2d_to_3d(circle, z=z_pos, zdir='z')
    
    # Create rectangle
    rect = Rectangle((0.1, 0.6), 0.3, 0.2, color=color, alpha=0.4)
    ax.add_patch(rect)
    art3d.pathpatch_2d_to_3d(rect, z=z_pos + 0.5, zdir='z')

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 3)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Complex 3D Patch Collection')

plt.show()

Key Parameters

Parameter Description Example
z Position along the specified direction z=0
zdir Direction to extrude the patch zdir='x', zdir='y', zdir='z'
alpha Transparency level (0-1) alpha=0.7

Conclusion

Use pathpatch_2d_to_3d() to convert 2D patches into 3D objects. Vary the zdir parameter to position patches on different planes, and use different colors and transparency for better visualization.

Updated on: 2026-03-25T23:57:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements