Subplots & Layouts

Matplotlib is a Python library for creating charts and visualizations — and subplots let you arrange several charts in a grid inside one figure so they can be compared at a glance.

Learn Subplots & Layouts in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll create a grid with plt.subplots(), draw on each cell, share axes, and tidy the spacing with tight_layout.

plt.subplots(rows, cols) builds a grid in one call and returns the figure and an array of axes . You then draw on each cell with axs[row, col] — a more explicit, object-oriented style than the plain plt. calls.

What you'll see: one figure split into four panels. The top row shows a sine and cosine wave, the bottom row a straight line and a square-root curve, each with its own title, and tight_layout keeps them neatly spaced.

When plots share a scale, pass sharex=True or sharey=True so their axes line up and duplicate tick labels disappear. Add one overarching title with fig.suptitle() .

What you'll see: two charts stacked vertically. The top one rises steeply (an exponential) while the bottom one flattens out (a logarithm). They share one x-axis at the bottom, and a single bold title sits above the whole figure.

Each axes is independent, so a single figure can mix a line plot, a bar chart, and a scatter plot. A 1×3 row is perfect for a quick dashboard.

What you'll see: three panels side by side in a single wide figure — a line on the left, an orange bar chart in the middle, and a green scatter cloud on the right — all under one shared "Mini Dashboard" title.

Replace each ___ to build a 1×2 grid and tidy the spacing.

❌ TypeError: 'AxesSubplot' object is not subscriptable

With a single row or column, axs is 1D — use axs[0] , not axs[0, 0] .

Call plt.tight_layout() after drawing everything to space the panels out.

Build a side-by-side figure with a line plot and a bar chart, each titled, under one super-title.

Lesson 11 complete — you can build multi-panel figures!

You created grids with plt.subplots(), drew on each cell, shared axes, added a figure title, and used tight_layout to keep everything spaced cleanly.

🚀 Up next: Styling & Themes — give your charts a polished, consistent look.

Practice quiz

What does plt.subplots(2, 2) return?

  • The figure and an array of axes
  • Only a single axes
  • A list of figures
  • The pixel dimensions of the grid

Answer: The figure and an array of axes. plt.subplots(rows, cols) returns the figure and an array of axes.

How do you draw on the top-right cell of a 2x2 grid?

  • axs.plot(0, 1)

For a 2D grid, index the axes array like axs[0, 1] for the top-right cell.

Which argument makes subplots share the same x-axis scale?

  • linkx=True
  • samex=True
  • sharex=True
  • joinx=True

Answer: sharex=True. Passing sharex=True aligns the x-axes and removes duplicate inner tick labels.

How do you add one title above all the subplots?

  • ax.set_title('...')
  • plt.title('...')
  • axs.title('...')
  • fig.suptitle('...')

Answer: fig.suptitle('...'). fig.suptitle() places a single super-title at the top of the whole figure.

What sets the overall figure dimensions in plt.subplots()?

  • size=(10, 8)
  • figsize=(10, 8)
  • dimensions=(10, 8)
  • shape=(10, 8)

Answer: figsize=(10, 8). figsize=(width, height) sets the figure size in inches.

Which call auto-spaces panels so titles stop overlapping?

  • plt.fix()
  • plt.spacing()
  • plt.tight_layout()
  • plt.adjust()

Answer: plt.tight_layout(). plt.tight_layout() adjusts spacing after you've drawn everything.

For plt.subplots(1, 3), how is the axes array shaped?

  • A scalar axes

A single row gives a 1D array, so you index it with axs[0], axs[1], axs[2].

Can a single figure mix a line, a bar, and a scatter plot?

  • Yes — each axes is independent
  • No, all subplots must be the same type
  • Only with separate figures
  • Only if sharex=True

Answer: Yes — each axes is independent. Each axes is independent, so one figure can mix different chart types.

What error does axs[0, 0] raise on a single-row subplot result?

  • KeyError: 0
  • TypeError: 'AxesSubplot' object is not subscriptable
  • ValueError: bad index
  • IndexError: out of range

Answer: TypeError: 'AxesSubplot' object is not subscriptable. With one row, axs is 1D — use axs[0], not axs[0, 0].

What does the 'rows' value in plt.subplots(rows, cols) control?

  • The figure title
  • The colormap
  • The number of horizontal rows of plots
  • The line color

Answer: The number of horizontal rows of plots. The first argument is the number of rows in the subplot grid.