Reputation: 84
Matplotlib's broken axis example has a break on the left and the right side of the frame and is placed exactly in the middle of the y-axis.
Any help is greatly appreciated.
Upvotes: 1
Views: 961
Reputation: 5389
transforms
to get the relevant corners of the two axes in the axes coordinates of one of the axes# Get the points from the lower-right corner of ax1
# to the top-right corner of ax2 in the axes coordinates
# of ax1
low = (1, 0)
high = ax1.transAxes.inverted().transform(
ax2.transAxes.transform((1, 1))
)
and then plotting them with transform=ax1.transAxes
ax1.plot(
*list(zip(low, high)), "-k",
transform=ax1.transAxes, clip_on=False,
lw=ax1.spines.right.get_linewidth(),
)
2 and 3. Adjust the whitespace and position of the break This can be achieved using any way of creating different axes in matplotlib
, such as passing gridspec_kw
to plt.subplots
e.g.
gridspec_kw = dict(height_ratios=(1, 4), hspace=0.30)
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, gridspec_kw=gridspec_kw)
As the name suggest height_ratios
defines the ratios of the heights of the axes. hspace
sets the vertical space between the two sets of axes (if you want to break the x-axis instead you can use wspace
)
Applying these changes to the example you link, and removing the diagonal lines on the right hand side by changing
ax1.plot([0, 1], [0, 0], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 1], [1, 1], transform=ax2.transAxes, **kwargs)
to
ax1.plot(0, 0, transform=ax1.transAxes, **kwargs)
ax2.plot(0, 1, transform=ax2.transAxes, **kwargs)
Upvotes: 1