0Pricing
Python Academy · Lesson

Subplots and Layouts

Combine multiple charts.

Subplots and Layouts is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Multiple Charts

Often one chart is not enough. You may want to compare several views side by side, or break a busy plot into smaller panels. Matplotlib calls this arranging multiple axes inside one figure.

The main tool is plt.subplots(rows, cols).

A Grid of Axes

fig, axes = plt.subplots(2, 3) creates a figure with a 2 by 3 grid, giving you six axes. The axes result is a 2D array you index like axes[0][1].

The total panel count is just rows times columns.

rows, cols = 2, 3
total = rows * cols
print('Grid', rows, 'x', cols, '=', total, 'axes')
for r in range(rows):
    for c in range(cols):
        print('panel at', (r, c))

Indexing the Axes Array

With a single row, axes is one-dimensional: use axes[0], axes[1]. With a full grid it is two-dimensional: axes[row][col].

For a single subplot, no indexing is needed at all.

Iterating Over Panels

A clean pattern is to flatten the grid and loop, pairing each axes with its data. This avoids repeating nearly identical code for each panel.

datasets = ['A', 'B', 'C', 'D']
# imagine axes flattened to a list of 4
for i, name in enumerate(datasets):
    print('Plot dataset', name, 'on flat axes index', i)

Shared Axes

When panels share a scale, pass sharex=True or sharey=True. The subplots then use one common range, which makes comparisons fair and removes redundant tick labels.

This is ideal for stacked time series.

Figure Size for Grids

A grid needs more room than a single plot. Set figsize=(width, height) in inches when calling subplots. A rule of thumb is to scale the size with the number of columns and rows.

cols, rows = 3, 2
per_panel_w, per_panel_h = 3, 2.5
print('Suggested figsize:', (cols * per_panel_w, rows * per_panel_h))

Spacing Between Panels

Panels can crowd each other. fig.tight_layout() fixes most cases automatically. For finer control use fig.subplots_adjust(wspace=0.4, hspace=0.4) to set horizontal and vertical gaps.

Larger values mean more space between subplots.

Uneven Layouts with GridSpec

Not every layout is a uniform grid. GridSpec lets one axes span multiple rows or columns, for example a wide chart on top and two small ones below.

You define the grid, then slice it like an array to place each axes.

Planning Spans

Think of GridSpec slices like ranges. A top chart spanning all columns is gs[0, :]; a bottom-left chart is gs[1, 0]. Planning the slices on paper first avoids confusion.

layout = {
    'header': '[0, :]   (full width)',
    'left':   '[1, 0]   (bottom left)',
    'right':  '[1, 1]   (bottom right)',
}
for name, slot in layout.items():
    print(name.ljust(7), slot)

One Figure, One Story

A multi-panel figure should still tell a single coherent story. Use a shared fig.suptitle(), consistent colors, and shared scales so the panels read as a unit rather than a random collage.

Per-Panel Customization

Because each cell is its own axes, you customize them independently. axes[0][0].set_title('Sales') titles only the top-left panel, while axes[1][1].set_ylabel('Cost') labels only the bottom-right.

This independence is exactly why the object-oriented style shines with grids.

Quick Check

Test your grasp of multi-panel layouts.

Recap

You can now combine charts:

  • plt.subplots(rows, cols) builds a grid of axes
  • Index with axes[row][col] or flatten and loop
  • sharex/sharey align scales; figsize sizes the grid
  • tight_layout and subplots_adjust manage spacing
  • GridSpec enables panels that span multiple cells

Frequently asked questions

Is the “Subplots and Layouts” lesson free?

Yes — the full text of “Subplots and Layouts” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Subplots and Layouts”?

Combine multiple charts. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Subplots and Layouts” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Figures and Axes
  2. Line, Bar and Scatter Plots
  3. Customizing Plots
  4. Subplots and Layouts
← Back to Python Academy