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)
fig, axd = plt.subplot_mosaic("""AABCCB""")axd["A"].plot(x, y) # 'A' spans the top-left 2 cellsaxd["B"].hist(data) # 'B' spans the full right columnaxd["C"].bar(cats, v)
GridSpec
from matplotlib.gridspec import GridSpecfig = plt.figure(figsize=(10, 8))gs = GridSpec(2, 2, figure=fig)ax_big = fig.add_subplot(gs[0, :]) # span the whole top rowax_bl = fig.add_subplot(gs[1, 0])ax_br = fig.add_subplot(gs[1, 1])
Key takeaways
- 01subplot_mosaic lets you describe an unequal layout as ASCII art of labels.
- 02Repeated labels make an Axes span multiple grid cells.
- 03GridSpec is the classic API: slice gs[row, col] to place and span Axes.
- 04Use these for dashboards with a mix of large and small panels.
