Explain Codes LogoExplain Codes Logo

Matplotlib Different Size Subplots

python
matplotlib
subplots
grid-spec
Alex KataevbyAlex Kataev·Sep 5, 2024
TLDR

Craft diverse-sized subplots using Matplotlib's GridSpec. Use rowspan and colspan for controlling the span of subplots across rows and columns respectively. Here's a quick snippet to get you started:

import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec # A 3x3 grid layout is being set up gs = GridSpec(3, 3, figure=plt.figure(figsize=(8, 6))) # A large subplot occupying the top-left plt.subplot(gs[0:2, 0:2]) # What's cooking good looking? # A horizontal subplot at the bottom plt.subplot(gs[2, :]) # Horizontal, because we like panorama! # A vertical subplot on the upper-right side plt.subplot(gs[0:2, 2]) # The lean, mean, vertical machine! plt.show()

Deeper Down The Rabbit Hole: GridSpec and rowspan/colspan

When GridSpec feels limiting, remember it packs a punch with rowspan and colspan for superior subplot control. Let's see it in action with a tasteful layout below:

import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec fig, ax = plt.subplots(figsize=(10, 6)) gs = GridSpec(2, 3, figure=fig, width_ratios=[3, 1, 1], height_ratios=[1, 2]) # First subplot spanning the full width of the top row ax1 = fig.add_subplot(gs[0, :]) # Second subplot spanning the first 2 columns of the bottom row ax2 = fig.add_subplot(gs[1, 0:2]) # Third and the largest subplot in the bottom-right corner ax3 = fig.add_subplot(gs[1, 2]) # Because everybody loves a corner seat! plt.tight_layout() # Pulling up the pants of our plot plt.show()

Fine-tuning Your Subplots

Manually Controlling Subplot Dimensions

Get hands-y with your subplots and manually adjust their dimensions using the axes object parameters, not efficient, but hey you call the shots!

fig = plt.figure() ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.4]) # Making room for a big fat plot ax2 = fig.add_axes([0.3, 0.6, 0.4, 0.3]) # Squeezed in, but still pretty fly! plt.show()

The Power of Python: subplot2grid

Invoke the power of Python's grid control with subplot2grid for precise cell access and size control, feeling like a plotting wizard yet?

ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=2) # Stretching across 2 columns like a boss! ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2) # Rowspan and Colspan, double trouble! ax3 = plt.subplot2grid((3, 3), (0, 2), rowspan=3) # One lean column for me, thank you! plt.show()

Size Matters: Control Figure Size

figsize, a nifty parameter, controls the overall figure size. A larger figsize can give your beautiful subplots the space they deserve.

Plot and Share

You've orchestrated a masterpiece with subplot positioning. Time to show it off! Save your figures as a PDF using fig.savefig, perfect for print or digital sharing.

fig.savefig('my_subplots.pdf') # Your plots, wherever you go!

Doctor's Orders: Tips, Tricks, and Fixes

Quickfire Shortcuts for Speed-Demons

  • Lightning-fast grid layout: fig, axs = plt.subplots(2, 2)
  • Sync axis limits with: sharex=True or sharey=True

Watch Your Step!

  • Subplots can overlap: plt.tight_layout() to the rescue!
  • Beware of messed up ratios: if it looks stretched, rework subplot dimensions.

Pythonic Plotting Practices

  • Complex plots? Go object-oriented.
  • fig.add_subplot() adds plots to specific grid positions.