Mark a visible axis break

How do I show a visible break in a Matplotlib y axis?

In Matplotlib, use two stacked axes sharing x, with y limits 0–8 and 94–107, and draw break marks at their adjoining spines.

On this page

Published and tested 22 November 2026.

Which code change produces this result?

The six input values include four small observations and two near 100. I draw the same series on both axes, hide the adjoining spines, and use axes-coordinate diagonal marks with clip_on=False. State the omitted interval explicitly.

This article uses simulated example data.

Original output: The full y range from the small observations to values near 100 compresses the lower points.
Open full-size image
Original output. The full y range from the small observations to values near 100 compresses the lower points.
Result output: The corrected display omits y=8 to 94 and marks the break at both sides. Small values are distinguishable, while the two large observations keep their actual y-axis labels.
Open full-size image
Result output. The corrected display omits y=8 to 94 and marks the break at both sides. Small values are distinguishable, while the two large observations keep their actual y-axis labels.

What changes in the actual output?

The corrected display omits y=8 to 94 and marks the break at both sides. Small values are distinguishable, while the two large observations keep their actual y-axis labels.

indexvalue
02
13
24
35
498
5103

The table shows all input rows. (6)

What should I check before using this setting?

Mark every omitted range and keep an unbroken version available. Read values against each panel's scale when comparing magnitudes.

How can I reproduce the figures?

Run python example.py in a writable folder. Tested with Python 3.12.4, Matplotlib 3.8.4 and NumPy 1.26.4. The script writes data.csv, before.png, after.png and after.pdf beside itself.

These are the code, data and result files used in this article. Choose a file below to run the example or compare your results.

  • example.pyComplete example code to run on your computer.
  • data.csvThe input data used in the example; open it as a table.
  • before.pngThe figure before the change, for comparison.
  • after.pngThe figure after applying the settings in this article.
  • after.pdfThe result in PDF format, for viewing and page layout.
  • Usage license · LICENSE.txtTerms for reusing the code, data and figures, with source and font notices.
File verification
  • hashes.jsonChecksums for checking whether the downloaded files are complete.

Complete Python code

# Original Bopbun code/data: CC0-1.0. Font rights remain separate; see LICENSE.txt.
# Tested: Python 3.12.4, Matplotlib 3.8.4, NumPy 1.26.4.
from pathlib import Path
import csv
import json
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import colors, ticker, dates
OUT=Path(__file__).resolve().parent
plt.rcdefaults()
plt.rcParams.update({'font.family':'DejaVu Sans','font.size':11,'figure.dpi':120,
                     'savefig.dpi':160,'pdf.fonttype':42})
def data_file(header,rows):
    with (OUT/'data.csv').open('w',newline='',encoding='utf-8') as f:
        w=csv.writer(f);w.writerow(header);w.writerows(rows)
def canvas(**kw):
    return plt.subplots(figsize=(7.2,4.5),layout='constrained',**kw)

def make(fixed):
    x=np.arange(6);y=np.array([2,3,4,5,98,103]);data_file(['index','value'],zip(x,y))
    if not fixed:
        fig,ax=canvas();ax.plot(x,y,'o');ax.set(xlabel='Index',ylabel='Response (a.u.)',title='Full range');return fig
    fig,(upper,lower)=plt.subplots(2,1,sharex=True,figsize=(7.2,4.5),layout='constrained',gridspec_kw={'height_ratios':[1,2]})
    for ax in [upper,lower]:ax.plot(x,y,'o');ax.set_ylabel('Response (a.u.)')
    upper.set_ylim(94,107);lower.set_ylim(0,8);upper.spines['bottom'].set_visible(False);lower.spines['top'].set_visible(False);upper.tick_params(bottom=False,labelbottom=False)
    for ax,ypos in [(upper,0),(lower,1)]:
        for xpos in [0,1]:ax.plot([xpos-.012,xpos+.012],[ypos-.025,ypos+.025],transform=ax.transAxes,color='black',clip_on=False)
    upper.set_title('Visible axis break: omitted range 8 to 94');lower.set_xlabel('Index');return fig

if __name__=='__main__':
    before=make(False);after=make(True)
    before.savefig(OUT/'before.png');after.savefig(OUT/'after.png')
    after.savefig(OUT/'after.pdf',metadata={'CreationDate':None,'ModDate':None})
    after.canvas.draw()
    print(json.dumps({'axes':len(after.axes),'limits':[{'x':list(a.get_xlim()),'y':list(a.get_ylim())} for a in after.axes]}))
    plt.close('all')

Which sources and licenses apply?

Original code and data: CC0-1.0. Figures use DejaVu Sans; its font license remains separate. Install Matplotlib and NumPy locally to run the code.

Which related answer do I need?

Research figure questions