Skip to main content
Albero Academy

Complex Layouts

Unequal subplot sizes with GridSpec and mosaic layouts.

Figures & Layout8 min

A uniform grid isn't always right — sometimes you want one big plot and two small ones, or a chart spanning two columns. GridSpec and the newer subplot_mosaic give you unequal, spanning layouts.

subplot_mosaic (the easy way)

describe the layout as ASCII art
fig, axd = plt.subplot_mosaic(
"""
AAB
CCB
"""
)
axd["A"].plot(x, y) # 'A' spans the top-left 2 cells
axd["B"].hist(data) # 'B' spans the full right column
axd["C"].bar(cats, v)

GridSpec

the classic API
from matplotlib.gridspec import GridSpec
fig = plt.figure(figsize=(10, 8))
gs = GridSpec(2, 2, figure=fig)
ax_big = fig.add_subplot(gs[0, :]) # span the whole top row
ax_bl = fig.add_subplot(gs[1, 0])
ax_br = fig.add_subplot(gs[1, 1])

Key takeaways

  1. 01subplot_mosaic lets you describe an unequal layout as ASCII art of labels.
  2. 02Repeated labels make an Axes span multiple grid cells.
  3. 03GridSpec is the classic API: slice gs[row, col] to place and span Axes.
  4. 04Use these for dashboards with a mix of large and small panels.

Finished this chapter?

Mark it complete to track your progress through Matplotlib.