PYTHON Tutorial

Working with Multiple Plots

Subplots:

  • Divide a figure into multiple subplots to display multiple related plots.
  • Use plt.subplot(nrows, ncols, index) to create subplots.

Multiple Figures:

  • Create separate figures for unrelated plots.
  • Use plt.figure(num) to create a new figure.

Grid Spec:

  • A flexible system for customizing subplot layouts.
  • Use plt.subplots(nrows, ncols, gridspec_kw=dict(height_ratios=[...], width_ratios=[...])) to define subplot layout.

Python Example:

Creating and Managing Multiple Plots

import matplotlib.pyplot as plt

# Create a figure with 2 rows and 2 columns of subplots
fig, axs = plt.subplots(2, 2)

# Add data to each subplot
axs[0, 0].plot([1, 2, 3], [4, 5, 6])
axs[0, 1].plot([7, 8, 9], [10, 11, 12])
axs[1, 0].plot([13, 14, 15], [16, 17, 18])
axs[1, 1].plot([19, 20, 21], [22, 23, 24])

# Adjust subplot spacing
plt.subplots_adjust(wspace=0.3, hspace=0.3)

# Show the plot
plt.show()