Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - Show axis break // in graph

I am trying to recreate the look of the following plot:

enter image description here

The breaking of the axis itself is quite straightforward as described here: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/broken_axis.html

But I have no idea how to include the // break in the graph itself. Any ideas?

Here is how it currently looks with my data.

Here

like image 722
Tintallë Avatar asked Oct 30 '25 05:10

Tintallë


2 Answers

The package brokenaxes is perfect for breaks in the axes as you are aware but it does not seem there is functionality for this in a published package. I have however figured out how to achieve what you are looking for.

First install the package from your environment directory in terminal:

>>> pip install brokenaxes

or

>>> conda install -c conda-forge brokenaxes

Solution:

from brokenaxes import brokenaxes
import numpy as np
import matplotlib.pyplot as plt

x_min    = 0
x_break  = 3
gap      = .1
x_max    = 6

def f(x):
    return np.cos(x)


fig  = plt.figure(figsize=(10,4))
brax = brokenaxes(xlims=((x_min, x_break), (x_break+gap, x_max)), hspace=gap)
xs   = np.linspace(x_min, x_max, 150)

brax.plot(xs, f(xs))
brax.annotate(u'—', (x_break-0.001, f(x_break)), rotation=45, size=25, ha='center', va='center', c='C0')
brax.annotate(u'—', (x_break+gap, f(x_break)), rotation=45, size=25, ha='center', va='center', c='C0')

plt.show()

If you are plotting from a an existing array, the function f could retrieve these values indexing from the array.

like image 156
foam78 Avatar answered Nov 01 '25 19:11

foam78


Use the example link https://matplotlib.org/stable/gallery/subplots_axes_and_figures/broken_axis.html, to achieve your target:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(19680801)

# creat data points
x = np.arange(20)+1
pts = x/x

# If we were to simply plot pts, we'd lose most of the interesting
# details due to the outliers. So let's 'break' or 'cut-out' the y-axis
# into two portions - use the top (ax1) for the outliers, and the bottom
# (ax2) for the details of the majority of our data
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
fig.subplots_adjust(hspace=0.05)  # adjust space between axes

# plot the same data on both axes
ax1.plot(x, pts)
ax2.plot(x, pts)

# zoom-in / limit the view to different portions of the data
ax1.set_xlim(0, 10)  # left part plot
ax2.set_xlim(11, 19)  # right part plot

# hide the spines between ax and ax2
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
#ax1.xaxis.tick_top()
#ax1.tick_params(labeltop=False)  # don't put tick labels at the top
ax2.yaxis.tick_right()

# Now, let's turn towards the cut-out slanted lines.
# We create line objects in axes coordinates, in which (0,0), (0,1),
# (1,0), and (1,1) are the four corners of the axes.
# The slanted lines themselves are markers at those locations, such that the
# lines keep their angle and position, independent of the axes size or scale
# Finally, we need to disable clipping.

d = .9  # proportion of vertical to horizontal extent of the slanted line
kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
              linestyle="none", color='k', mec='k', mew=1, clip_on=False)
ax1.plot([1, 1], [0, 1], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 0], [0, 1], transform=ax2.transAxes, **kwargs)

# add slanted line on the graph
ax1.plot([1], [0.5], transform=ax1.transAxes, **kwargs)
ax2.plot([0], [0.5], transform=ax2.transAxes, **kwargs) 
#fig.savefig('_')

plt.show()

the result like enter image description here

like image 29
ginger21 Avatar answered Nov 01 '25 20:11

ginger21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!