Break in X-Axis

Hi,

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

I will need to have graphs with a break in the x-axis, comparable to this
one
http://www.originlab.com/www/Products/images/Date_Axis_plot_with_XY_break_500px.png

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}
matplotlib.rcParams.update(params)

# 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']:
            spine.set_color('none')
        else:
            spine.set_position(('outward',0))
    plt.plot(zip(*y)[0], zip(*y)[1])
    ax1.set_xlim(740,820)
    plt.xticks([750,800])

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']:
            spine.set_color('none')
        else:
            spine.set_position(('outward',0))
    plt.plot(zip(*y)[0], zip(*y)[1])
    ax2.set_xlim(930, 1010)
    ax2.set_xticks([950,1000])
    ax2.set_yticks([])
    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']:
            spine.set_color('none')
        else:
            spine.set_position(('outward',0))
    ax3.set_yticks([])
    ax3.set_xticks([])
    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
.eps!
    shift = spacing/2
    # the four small lines
    line =
matplotlib.lines.Line2D([0.5-0.005-shift,0.5+0.005-shift],[-0.013,0.013],
linewidth=1, color='black', clip_on=False)
    ax3.add_line(line)
    line =
matplotlib.lines.Line2D([0.5-0.005+shift,0.5+0.005+shift],[-0.013,0.013],
linewidth=1, color='black', clip_on=False)
    ax3.add_line(line)
    line =
matplotlib.lines.Line2D([0.5-0.005-shift,0.5+0.005-shift],[1+-0.013,1+0.013],
linewidth=1, color='black', clip_on=False)
    ax3.add_line(line)
    line =
matplotlib.lines.Line2D([0.5-0.005+shift,0.5+0.005+shift],[1+-0.013,1+0.013],
linewidth=1, color='black', clip_on=False)
    ax3.add_line(line)

plt.show()
#plt.savefig('breaktest.png')
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 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: http://old.nabble.com/file/p27624781/image.eps image.eps
PNG output: http://old.nabble.com/file/p27624781/matplotlib_axis_break.png
matplotlib_axis_break.png
Raw Data: http://old.nabble.com/file/p27624781/532_0.1_10.txt
532_0.1_10.txt

···

--
View this message in context: http://old.nabble.com/Break-in-X-Axis-tp27624781p27624781.html
Sent from the matplotlib - users mailing list archive at Nabble.com.