Inset & Broken Axes

An inset axes is a small zoomed-in plot drawn inside a larger one, and a broken axis cuts a gap out of a scale so distant data clusters can share a single chart without wasting empty space.

Learn Inset & Broken Axes in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

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 add a zoom-in with ax.inset_axes() , mark the zoomed region with indicate_inset_zoom() , and hand-build a broken axis from two subplots with diagonal break marks.

ax.inset_axes([x, y, w, h]) drops a miniature Axes inside the main one. The four numbers are in axes-fraction coordinates: position the bottom-left corner at (x, y) and give it width w and height h . Plot the same data on the inset, then narrow its limits to the region you want to magnify.

What you'll see: a damped sine curve filling the plot with a small framed copy in the upper-right that magnifies just the first peak, revealing detail that is hard to read in the full view.

A floating zoom is clearer when the reader can see where it came from. After setting the inset's limits, call ax.indicate_inset_zoom(axins) . Matplotlib draws a rectangle on the main axes around the zoom region and connector lines to the inset.

Matplotlib has no one-line broken axis, but the recipe is reliable: stack two subplots that share the x-axis, give the top one the high range and the bottom one the low range, hide the facing spines, and draw small diagonal marks where the break sits.

What you'll see: two short bars and two towering bars sharing one chart — the y-axis has a visible gap with little diagonal slashes, so the huge values and the tiny values are both readable without one dwarfing the other.

Replace each ___ to add an inset and zoom it into the data's first second.

You created the inset but plotted only on the main axes. ✅ Call axins.plot(...) on the inset too, then set its limits.

You called it before narrowing the inset. ✅ Set axins.set_xlim/ylim first so it knows the region.

They are clipped to the axes. ✅ Pass clip_on=False and draw them in transAxes coordinates.

Plot a curve with a sharp spike, add an inset zoomed onto the spike, and link it with indicate_inset_zoom.

Lesson complete — you can zoom and break axes!

You added a zoom-in inset, linked it to its region with indicate_inset_zoom, and hand-built a broken axis from two subplots with diagonal break marks.

🚀 Up next: Drawing Shapes (Patches) — add rectangles, circles, and arrows to annotate regions of your plots.

Practice quiz

What does ax.inset_axes([x, y, w, h]) create?

  • A new figure window
  • A small inset axes inside the main axes
  • A colorbar
  • A second x-axis

Answer: A small inset axes inside the main axes. inset_axes drops a miniature Axes inside the parent axes.

In what coordinate system are the four inset_axes numbers given?

  • Pixels
  • Data units
  • Inches
  • Axes-fraction coordinates

Answer: Axes-fraction coordinates. [x, y, w, h] are in axes-fraction coordinates of the parent axes.

How do you restrict an inset to a zoom region?

  • Set its xlim and ylim
  • Call inset.zoom()
  • Pass zoom=True
  • Use plt.magnify()

Answer: Set its xlim and ylim. You narrow the inset with axins.set_xlim() and set_ylim() to the region of interest.

What does ax.indicate_inset_zoom(axins) draw?

  • A colorbar
  • A second legend
  • A rectangle and connector lines linking the region to the inset
  • A title

Answer: A rectangle and connector lines linking the region to the inset. It marks the zoom region with a rectangle and connectors to the inset.

When must you set the inset's limits relative to indicate_inset_zoom?

  • After it
  • Before it, so it knows the region
  • It does not matter
  • Only inside a loop

Answer: Before it, so it knows the region. indicate_inset_zoom reads the inset's xlim/ylim, so set them first.

Does Matplotlib have a single built-in broken-axis function?

  • Yes, plt.brokenaxis()
  • Yes, ax.break_axis()
  • Yes, plt.gap()
  • No, you build it from two subplots

Answer: No, you build it from two subplots. There is no one-liner; the recipe uses two subplots sharing an axis.

Which argument links two subplots to share their x-axis for a break?

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

Answer: sharex=True. plt.subplots(2, 1, sharex=True) links the x-axes of the stacked panels.

How do you open the visual gap between the two broken-axis panels?

  • Increase the DPI
  • Use a log scale
  • Hide the facing spines with spine.set_visible(False)
  • Call ax.gap()

Answer: Hide the facing spines with spine.set_visible(False). Hiding the bottom spine of the top panel and the top spine of the bottom panel opens the break.

Why pass clip_on=False when drawing the diagonal break marks?

  • To color them red
  • So they are not clipped at the axes edge
  • To make them dashed
  • To rotate them

Answer: So they are not clipped at the axes edge. The marks sit at the axes boundary, so clip_on=False keeps them from being clipped.

When is a broken axis preferable to a log scale?

  • When values span many orders of magnitude smoothly
  • When all values are negative
  • When you want a circular plot
  • When two clusters are separated by a large empty gap

Answer: When two clusters are separated by a large empty gap. A broken axis compresses an empty gap between two clusters without distorting spacing within them.