How to Make a Broken Axis Plot in Matplotlib

Skip an empty range on the y-axis or x-axis by splitting a Matplotlib chart into two subplots with different limits and drawing the break marks.

Matplotlib has no single "break the axis" function. To make a broken axis, draw the same data on two subplots, give each one a different range with set_ylim(), hide the spines between them, and add diagonal marks to show the break.

Quick Example

One outlier would squash every other bar. Splitting the y-axis keeps both readable:

import matplotlib.pyplot as plt

services = ["Auth", "Search", "Cart", "Billing", "Export", "Email", "Images"]
latency = [42, 118, 65, 88, 1460, 54, 131]

fig, (top, bottom) = plt.subplots(
    2, 1, sharex=True, figsize=(8, 5), height_ratios=[1, 2], layout="constrained")

# The same bars on both axes, each showing a different range
for ax in (top, bottom):
    ax.bar(services, latency, color="#577590")

top.set_ylim(1350, 1500)
bottom.set_ylim(0, 160)

# Remove the edge between the two panels
top.spines["bottom"].set_visible(False)
bottom.spines["top"].set_visible(False)
top.tick_params(bottom=False)

# Diagonal marks at the ends of the cut
mark = dict(marker=[(-1, -0.6), (1, 0.6)], markersize=11, linestyle="none",
            color="black", markeredgewidth=1.2, clip_on=False)
top.plot([0, 1], [0, 0], transform=top.transAxes, **mark)
bottom.plot([0, 1], [1, 1], transform=bottom.transAxes, **mark)

top.set_title("Response time by service")
fig.supylabel("Milliseconds")
plt.show()

Bar chart with a broken y-axis: six services respond in 42 to 131 milliseconds, and the Export bar continues through the break to 1,460 milliseconds

How the Break Marks Work

The marks are ordinary plot() calls with two special settings:

  • transform=ax.transAxes makes the coordinates relative to the plot, so (0, 0) is the bottom-left corner and (1, 1) is the top-right, whatever the data limits are.
  • marker=[(-1, -0.6), (1, 0.6)] defines a custom marker: a short line between those two points. Change 0.6 to make it steeper or flatter.

clip_on=False lets the marks extend past the edge of the plot. height_ratios=[1, 2] makes the lower panel twice as tall.

Keep the Scale the Same in Both Panels

In the example above, the top panel covers 150 ms in half the height that the bottom panel uses for 160 ms, so the slopes and bar segments aren't to the same scale. If that matters, make the height ratio match the ratio of the two ranges:

low, high = (0, 160), (1380, 1480)
ratios = [high[1] - high[0], low[1] - low[0]]

fig, (top, bottom) = plt.subplots(
    2, 1, sharex=True, figsize=(8, 5), height_ratios=ratios, layout="constrained")
for ax in (top, bottom):
    ax.bar(services, latency, color="#577590")
    ax.grid(axis="y", alpha=0.4)
    ax.set_axisbelow(True)

top.set_ylim(high)
bottom.set_ylim(low)
top.spines["bottom"].set_visible(False)
bottom.spines["top"].set_visible(False)
top.tick_params(bottom=False)

top.plot([0, 1], [0, 0], transform=top.transAxes, **mark)
bottom.plot([0, 1], [1, 1], transform=bottom.transAxes, **mark)

# Print the value so nobody has to read it across the break
top.bar_label(top.containers[0], labels=[f"{v:,}" if v > 1000 else "" for v in latency])
bottom.set_ylabel("Milliseconds")
plt.show()

Broken axis bar chart where both panels use the same milliseconds per inch, with gridlines and the outlier labeled 1,460

Break the X-Axis

For a gap along the x-axis, put the two panels side by side with sharey=True and flip which spines you hide. Here, two measurement sessions are separated by weeks with no data:

import numpy as np

rng = np.random.default_rng(11)
first = np.arange(0, 11)
second = np.arange(60, 71)
weight_first = 82 - 0.25 * first + rng.normal(0, 0.3, first.size)
weight_second = 76 - 0.08 * (second - 60) + rng.normal(0, 0.3, second.size)

days = np.concatenate([first, second])
weight = np.concatenate([weight_first, weight_second])

fig, (left, right) = plt.subplots(
    1, 2, sharey=True, figsize=(8, 4), layout="constrained")
for ax in (left, right):
    ax.plot(days, weight, "o", color="#bc4749")

left.set_xlim(-0.5, 10.5)
right.set_xlim(59.5, 70.5)
left.spines["right"].set_visible(False)
right.spines["left"].set_visible(False)
right.tick_params(left=False)

mark = dict(marker=[(-0.6, -1), (0.6, 1)], markersize=11, linestyle="none",
            color="black", markeredgewidth=1.2, clip_on=False)
left.plot([1, 1], [0, 1], transform=left.transAxes, **mark)
right.plot([0, 0], [0, 1], transform=right.transAxes, **mark)

left.set_ylabel("Weight (kg)")
fig.supxlabel("Day of study")
fig.suptitle("Two measurement sessions, seven weeks apart")
plt.show()

Scatter plot with a broken x-axis showing weight falling over days 0 to 10, then lower values on days 60 to 70

The points are plotted as markers only. A line plot would draw a segment from day 10 to day 60, which would show up as stray lines running off both panels.

Should You Break the Axis at All?

A break makes it harder to compare values by eye, especially with bars, whose length is supposed to show size. Other options:

Option Good when
Broken axis One or two values are far from the rest, and both groups need detail
Log scale Values span several orders of magnitude and ratios are what matter
Inset zoom You want the full picture plus a close-up of one region
Two separate charts The groups don't need to be compared directly

Here is the same latency data on a log scale:

fig, ax = plt.subplots(figsize=(8, 4), layout="constrained")
bars = ax.bar(services, latency, color="#577590")
ax.bar_label(bars, fmt="{:,.0f}", padding=2)
ax.set_yscale("log")
ax.set_ylim(10, 3000)
ax.set_ylabel("Milliseconds (log scale)")
plt.show()

The same seven services on a logarithmic y-axis with each bar labeled, showing all values without a break

Whichever you choose, say so on the chart: label the log axis, or keep the break marks obvious.

Similar Topics