How to add a 3d subplot to a matplotlib figure?

To add a 3D subplot to a matplotlib figure, you need to specify projection='3d' when creating the subplot. This enables three-dimensional plotting capabilities for visualizing data in 3D space.

Basic Steps

Follow these steps to create a 3D subplot ?

  • Import matplotlib and numpy libraries
  • Create x, y and z data points
  • Create a figure using plt.figure()
  • Add a subplot with projection='3d' parameter
  • Plot the 3D data using appropriate plotting methods
  • Display the figure with plt.show()

Example: Creating a 3D Line Plot

Here's how to create a basic 3D line plot ?

import matplotlib.pyplot as plt
import numpy as np

# Set the figure size
plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True

# Create x, y and z data points
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
z = x ** 2 + y ** 2

# Create figure and 3D subplot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot 3D line
ax.plot(x, y, z, color='red', linewidth=3)

# Add labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Line Plot')

plt.show()

Example: Multiple 3D Subplots

You can also create multiple 3D subplots in the same figure ?

import matplotlib.pyplot as plt
import numpy as np

# Create data
t = np.linspace(0, 4*np.pi, 100)
x = np.cos(t)
y = np.sin(t)
z1 = t
z2 = -t

# Create figure with two 3D subplots
fig = plt.figure(figsize=(12, 5))

# First 3D subplot
ax1 = fig.add_subplot(121, projection='3d')
ax1.plot(x, y, z1, color='blue', label='Spiral Up')
ax1.set_title('Ascending Spiral')
ax1.legend()

# Second 3D subplot  
ax2 = fig.add_subplot(122, projection='3d')
ax2.plot(x, y, z2, color='green', label='Spiral Down')
ax2.set_title('Descending Spiral')
ax2.legend()

plt.tight_layout()
plt.show()

3D Surface Plot Example

You can also create 3D surface plots using the same approach ?

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

# Create meshgrid data
x = np.linspace(-2, 2, 50)
y = np.linspace(-2, 2, 50)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2

# Create 3D surface plot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

# Plot surface
surface = ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)

# Add labels and colorbar
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Surface Plot')
fig.colorbar(surface)

plt.show()

Key Parameters

Parameter Description Example
projection='3d' Enables 3D plotting Required for all 3D plots
111 Subplot position (1x1 grid, position 1) 121 for 1x2 grid, position 1
figsize Figure dimensions in inches [8, 6] for 8x6 inches

Conclusion

Creating 3D subplots in matplotlib requires adding projection='3d' to the add_subplot() method. This enables various 3D plotting functions like line plots, surface plots, and scatter plots for effective data visualization in three dimensions.

Updated on: 2026-03-26T02:27:19+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements