Create a plot with broken axis
Scenario: Say we have profile data from the deep ocean. The profile is for the full depth of the ocean but most of the signal is in the upper ocean. We want to make a plot of the full profile but have a zoomed in look at the upper ocean. We can do this by manipulating the subplot feature.
First lets make up some data:
We will create a velocity profile that has the form:
$vel = A\, e^{-\lambda\,z} \cdot \sin \left(\frac{a \,\pi\,z}{D} \right)\cdot \sin \left(\frac{b\,\pi\,z}{D} \right)$
where $a$, $b$, $A$ and $\lambda$ are arbitrary constants. $D$ is the full depth of the profile.
import numpy as np
z = arange(0,4000,10)
D = max(z)
a,b,A = 5,75,10
lam = 0.01
vel = A*np.exp(-lam*z)*np.sin((a*pi*z)/D)*np.sin((b*pi*z)/D)
First, we plot the profile using a single plot:
import matplotlib.pylab as plt
#create figure
plt.figure()
#get axes handle
ax = plt.gca()
#flip y-axis
ax.invert_yaxis()
#plot data
plt.plot(vel,z)
#add labels
plt.xlabel('velocity profile')
plt.ylabel('Depth (m)')
plt.title('A velocity profile')
#display the plot
plt.show()
The plot looks fine but the information we care about is squashed in the upper portion of the plot. To showcase the signal more clearly, we will create a zoomed in version of the upper 500m. To do this we exploit the subplot function:
def brokenAxesDemo(vel,z):
#I am making this code into a function so that I can use it again in this demo.
#It's not a very useful function since a lot of things are hard coded.
#create a new figure with two subplots
fig,(ax1,ax2) = plt.subplots(2, 1, sharex=True)
#set the "zoom" or the y-limits on each subplots
ax1.set_ylim(0,500)
ax2.set_ylim(600,4000)
#set ytick marks for upper plot (optional, but the default tick marks may not look nice)
upper_yticks = np.arange(0,501,100)
ax1.set_yticks(upper_yticks)
#set ytick marks for lower plot (optional, but the default tick marks may not look nice)
lower_yticks = np.arange(600,4000,1000)
ax2.set_yticks(lower_yticks)
#invert the y-axis (since we are plotting profile data)
ax1.invert_yaxis()
ax2.invert_yaxis()
#now plot data into each subplot
ax1.plot(vel,z)
ax2.plot(vel,z)
#remove the bottom border from the top plot and the upper border from the bottom plot
ax1.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
#show only top x-axis tick marks on the top plot
ax1.xaxis.tick_top()
ax1.tick_params(labeltop='off') # hides the labels from the top x-axis
#show only bottom x-axis tick marks on the lower plot
ax2.xaxis.tick_bottom()
#squeeze plots closer
plt.subplots_adjust(hspace=0.2) #set to zero, if you want to join the two plots
#add figure labels
ax2.set_xlabel('velocity')
ax1.set_ylabel('Depth (m)')
ax2.set_ylabel('Depth (m)')
ax1.set_title('Velocity profile after zooming into upper 500m.')
return ax1, ax2
ax1,ax2 = brokenAxesDemo(vel,z)
So now we can see the upper ocean signal more clearly. All this has been pretty painless so far. We may also want to add some slashes to indicate where the y-axis is broken. Doing this requires a bit more work, but you can save yourself the trouble by copying the function below:
def addBreakClips(ax1,ax2):
""" Code to add diagonal slashes to truncated y-axes.
copied from http://matplotlib.org/examples/pylab_examples/broken_axis.html"""
d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax1.transAxes, color='r', clip_on=False)
ax1.plot((-d,+d),(-d,+d), **kwargs) # top-left diagonal
ax1.plot((1-d,1+d),(-d,+d), **kwargs) # top-right diagonal
kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d,+d),(1-d,1+d), **kwargs) # bottom-left diagonal
ax2.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-right diagonal
Let's remake the plot and add the slashes:
ax1,ax2 = brokenAxesDemo(vel,z)
addBreakClips(ax1,ax2)
Hopefully this demo makes sense. The trickiest part is probably choosing the right tick marks on the y-axis. It should be relatively easy to modify the code to create a broken x-axis.