Drawing multiple figures in parallel in Python with Matplotlib

To draw multiple figures in parallel in Python with Matplotlib, we can create subplots within a single figure window. This technique allows you to display multiple visualizations side by side for easy comparison.

Steps to Create Multiple Subplots

  • Create random data using numpy
  • Add subplots to the current figure using subplot(nrows, ncols, index)
  • Display data as an image using imshow() with different colormaps
  • Use show() to display the complete figure

Example

Here's how to create four subplots in a single row, each displaying the same data with different colormaps ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size for better visualization
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create random 5x5 data matrix
data = np.random.rand(5, 5)

# Create first subplot (1 row, 4 columns, position 1)
plt.subplot(1, 4, 1)
plt.imshow(data, cmap="Blues_r")
plt.title("Blues_r")

# Create second subplot (1 row, 4 columns, position 2)
plt.subplot(1, 4, 2)
plt.imshow(data, cmap="Accent_r")
plt.title("Accent_r")

# Create third subplot (1 row, 4 columns, position 3)
plt.subplot(1, 4, 3)
plt.imshow(data, cmap="terrain_r")
plt.title("terrain_r")

# Create fourth subplot (1 row, 4 columns, position 4)
plt.subplot(1, 4, 4)
plt.imshow(data, cmap="twilight_shifted_r")
plt.title("twilight_shifted_r")

# Display all subplots together
plt.show()

Alternative Approach Using subplots()

You can also use plt.subplots() for more control over the layout ?

import numpy as np
import matplotlib.pyplot as plt

# Create sample data
data = np.random.rand(5, 5)

# Create figure and subplots
fig, axes = plt.subplots(1, 4, figsize=(12, 3))

# Define colormaps
colormaps = ["Blues_r", "Accent_r", "terrain_r", "twilight_shifted_r"]

# Plot on each subplot
for i, (ax, cmap) in enumerate(zip(axes, colormaps)):
    ax.imshow(data, cmap=cmap)
    ax.set_title(cmap)
    ax.axis('off')  # Remove axes for cleaner look

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Example
nrows Number of rows in subplot grid 1 (single row)
ncols Number of columns in subplot grid 4 (four columns)
index Position of subplot (1-indexed) 1, 2, 3, 4
cmap Colormap for visualization "Blues_r", "terrain_r"

Conclusion

Use plt.subplot() to create multiple figures in parallel within a single window. The subplots() approach provides better control and is recommended for complex layouts with multiple visualizations.

Updated on: 2026-03-25T19:43:54+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements