Break in X-Axis


I am currently learning metplotlib to make beautiful figures for a LaTeX

I will need to have graphs with a break in the x-axis, comparable to this

As I found out, there is no native support for this. So I fiddled around a
lot and came up with the following solutions.

Please take this as a starting point if you happen to have the same
intensions with matplotlib.
Also I am happy for any input from more experienced users.

The code:

#@@@@@@@@@@@@@@@@@@@@@@@@@ begin
from matplotlib import rc
import numpy as np
import matplotlib.pyplot as plt

y = np.loadtxt('532_0.1_10.txt')
fig_width_pt = 400 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27 # Convert pt to inches
golden_mean = (np.sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt # width in inches
fig_height =fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]

rc('font',**{'family':'serif','serif':['Computer Modern Roman']})
params = {'backend': 'ps',
          'axes.labelsize': 10,
          'text.fontsize': 10,
          'legend.fontsize': 8,
          'xtick.labelsize': 8,
          'ytick.labelsize': 8,
          'text.usetex': True,
          'figure.figsize': fig_size,
          'axes.unicode_minus': True}

# setup figure with specified size
fig = plt.figure(figsize=fig_size)
width = 0.44 # width of the two subplots
spacing = 0.025/2 # spacing between subplots

if 1: # left subplot
    ax1 = plt.axes([0.08,0.15,width,0.8])
    for loc, spine in ax1.spines.iteritems():
        if loc in ['right']:
    plt.plot(zip(*y)[0], zip(*y)[1])

if 1: #right subplot with shared y-axis
    ax2 = plt.axes([0.08+width+spacing,0.15,width,0.8], sharey=ax1)
    for loc, spine in ax2.spines.iteritems():
        if loc in ['left', 'right']:
    plt.plot(zip(*y)[0], zip(*y)[1])
    ax2.set_xlim(930, 1010)
    ax2.set_ylim(0, 11000) # after second plot

if 1: # axes labels and break lines
    ax3 = plt.axes([0.08,0.15,2*width+0.025/2,0.8]) # change position to
adjust labels
    for loc, spine in ax3.spines.iteritems():
        if loc in ['left', 'bottom', 'top']:
    ax3.set_xlabel(r'Raman shift (cm-1)', labelpad = 25)
    # ax3.xaxis.set_label_coords(0.5,-0.06) #alternative to labelpad
    ax3.set_ylabel('Intensity (arb. unit)', labelpad = 10)
    ax3.patch.set_facecolor('None') # set background transparent. Must for
    shift = spacing/2
    # the four small lines
    line =
linewidth=1, color='black', clip_on=False)
    line =
linewidth=1, color='black', clip_on=False)
    line =
linewidth=1, color='black', clip_on=False)
    line =
linewidth=1, color='black', clip_on=False)
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@ end

And here some explanations:
I decided not to use subplots, but axes instead. I feel this gives more
control, even it involves arithmetics.

So there is one axes (ax1) for the left figure (no right axis)
then another axes (ax2) for the right figure (no left and right axis, sharey
with left figure)
And in the end a transparent axes (ax3) around both (only right axis), which
is used to plot the axis labels and the four small breaklines.

Looks better than Origin :wink:
EPS output: image.eps
PNG output:
Raw Data:


View this message in context:
Sent from the matplotlib - users mailing list archive at