Sometimes data has gaps or large jumps that make it hard to see details on a plot. Broken axes let you skip these gaps to focus on important parts.
Broken axes concept in Matplotlib
import matplotlib.pyplot as plt from matplotlib import gridspec fig = plt.figure() gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1], hspace=0.05) ax1 = fig.add_subplot(gs[0]) # Top plot ax2 = fig.add_subplot(gs[1], sharex=ax1) # Bottom plot # Hide x-axis labels on top plot plt.setp(ax1.get_xticklabels(), visible=False) # Plot data on ax1 and ax2 # Add diagonal lines to show break kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False) ax1.plot((-0.015, +0.015), (-0.02, +0.02), **kwargs) ax1.plot((1 - 0.015, 1 + 0.015), (-0.02, +0.02), **kwargs) kwargs.update(transform=ax2.transAxes) ax2.plot((-0.015, +0.015), (1 - 0.02, 1 + 0.02), **kwargs) ax2.plot((1 - 0.015, 1 + 0.015), (1 - 0.02, 1 + 0.02), **kwargs) plt.show()
This example uses gridspec to create two stacked plots sharing the x-axis.
Diagonal lines show the axis break visually.
import matplotlib.pyplot as plt from matplotlib import gridspec fig = plt.figure() gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1], hspace=0.05) ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1], sharex=ax1) ax1.plot([0, 1, 2, 3, 4], [0, 10, 20, 30, 40]) ax2.plot([0, 1, 2, 3, 4], [100, 110, 120, 130, 140]) plt.setp(ax1.get_xticklabels(), visible=False) kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False) ax1.plot((-0.015, +0.015), (-0.02, +0.02), **kwargs) ax1.plot((1 - 0.015, 1 + 0.015), (-0.02, +0.02), **kwargs) kwargs.update(transform=ax2.transAxes) ax2.plot((-0.015, +0.015), (1 - 0.02, 1 + 0.02), **kwargs) ax2.plot((1 - 0.015, 1 + 0.015), (1 - 0.02, 1 + 0.02), **kwargs) plt.show()
import matplotlib.pyplot as plt from matplotlib import gridspec fig = plt.figure(figsize=(6, 4)) gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1], hspace=0.1) ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1], sharex=ax1) x = [1, 2, 3, 4, 5] y1 = [10, 15, 20, 25, 30] y2 = [100, 110, 120, 130, 140] ax1.plot(x, y2, 'r-') ax2.plot(x, y1, 'b-') plt.setp(ax1.get_xticklabels(), visible=False) kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False) ax1.plot((-0.01, +0.01), (-0.02, +0.02), **kwargs) ax1.plot((1 - 0.01, 1 + 0.01), (-0.02, +0.02), **kwargs) kwargs.update(transform=ax2.transAxes) ax2.plot((-0.01, +0.01), (1 - 0.02, 1 + 0.02), **kwargs) ax2.plot((1 - 0.01, 1 + 0.01), (1 - 0.02, 1 + 0.02), **kwargs) plt.show()
This program creates a plot with two y-axis ranges separated by a break. It shows data points from 0 to 40 on the bottom plot and 100 to 140 on the top plot. The diagonal lines show where the axis is broken.
import matplotlib.pyplot as plt from matplotlib import gridspec # Create figure and gridspec with two rows fig = plt.figure(figsize=(6, 5)) gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1], hspace=0.05) # Create two subplots sharing x-axis ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1], sharex=ax1) # Data with a big gap between 40 and 100 x = [0, 1, 2, 3, 4] y_top = [100, 110, 120, 130, 140] y_bottom = [0, 10, 20, 30, 40] # Plot top data on ax1 ax1.plot(x, y_top, marker='o', color='red') ax1.set_ylim(90, 150) # Limit y-axis to top range # Plot bottom data on ax2 ax2.plot(x, y_bottom, marker='o', color='blue') ax2.set_ylim(-5, 45) # Limit y-axis to bottom range # Hide x tick labels on top plot plt.setp(ax1.get_xticklabels(), visible=False) # Add diagonal lines to indicate broken axis kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False) ax1.plot((-0.015, +0.015), (-0.02, +0.02), **kwargs) # left diagonal ax1.plot((1 - 0.015, 1 + 0.015), (-0.02, +0.02), **kwargs) # right diagonal kwargs.update(transform=ax2.transAxes) ax2.plot((-0.015, +0.015), (1 - 0.02, 1 + 0.02), **kwargs) # left diagonal ax2.plot((1 - 0.015, 1 + 0.015), (1 - 0.02, 1 + 0.02), **kwargs) # right diagonal plt.xlabel('X axis') ax2.set_ylabel('Value') ax1.set_ylabel('Value') plt.suptitle('Example of Broken Axes to Skip Gap in Data') plt.show()
Broken axes help focus on important data ranges by skipping large gaps.
Make sure to add visual cues like diagonal lines so viewers understand the axis is broken.
Sharing the x-axis keeps the plot aligned and easier to read.
Broken axes let you show separate data ranges in one figure by skipping gaps.
Use stacked subplots with shared axes and add diagonal lines to indicate breaks.
This technique helps reveal details hidden by large jumps in data.
Practice
broken axes in matplotlib plots?Solution
Step 1: Understand the concept of broken axes
Broken axes are used to skip over parts of the axis where data is not important or has large gaps.Step 2: Identify the main purpose
This helps to focus on important data ranges without showing the entire axis continuously.Final Answer:
To skip unimportant data ranges and focus on key parts -> Option AQuick Check:
Broken axes = skip gaps [OK]
- Thinking broken axes add 3D or animation
- Confusing broken axes with color changes
- Assuming broken axes zoom in automatically
brokenaxes package to use brokenaxes(...) directly for creating broken axes plots?Solution
Step 1: Recall the correct import syntax
Thebrokenaxespackage provides thebrokenaxesfunction to create broken axes plots.Step 2: Identify the correct import statement
The correct way isfrom brokenaxes import brokenaxesto access the function directly.Final Answer:
from brokenaxes import brokenaxes -> Option CQuick Check:
Import brokenaxes function = from brokenaxes import brokenaxes [OK]
- Using capital 'BrokenAxes' instead of 'brokenaxes'
- Trying to import from matplotlib instead of brokenaxes
- Using wrong import syntax causing ImportError
from brokenaxes import brokenaxes import matplotlib.pyplot as plt bax = brokenaxes(ylims=((0, 2), (8, 10))) bax.plot([0, 1, 2, 3], [1, 1.5, 9, 9.5]) plt.show()
Solution
Step 1: Analyze the ylims parameter
Theylims=((0, 2), (8, 10))means the y-axis is broken to show two ranges: 0 to 2 and 8 to 10, skipping 2 to 8.Step 2: Understand the plotted points
The y-values are 1, 1.5, 9, and 9.5, which fit into the two visible y-axis ranges.Final Answer:
A plot with y-axis broken between 2 and 8 showing points near 1 and 9 -> Option AQuick Check:
ylims tuple of ranges = broken y-axis [OK]
- Assuming y-axis is continuous without breaks
- Confusing x-axis and y-axis breaks
- Expecting an error from tuple ylims
from brokenaxes import brokenaxes import matplotlib.pyplot as plt bax = brokenaxes(xlims=(0, 2), (5, 7)) bax.plot([1, 6], [10, 20]) plt.show()
Solution
Step 1: Check the xlims argument syntax
The code usesxlims=(0, 2), (5, 7)which is invalid syntax; it should be a single tuple containing tuples likexlims=((0, 2), (5, 7)).Step 2: Confirm other parts are correct
The plot call and plt.show() are correct; the error is only in xlims syntax.Final Answer:
xlims should be a tuple of tuples, not separate tuples -> Option DQuick Check:
xlims syntax = tuple of tuples [OK]
- Passing multiple tuples without enclosing in one tuple
- Mixing up xlims and ylims syntax
- Calling plt.show() too early
brokenaxes?Solution
Step 1: Identify the axis to break
The gap is in y-values, so the y-axis needs to be broken to skip the large gap between 3 and 50.Step 2: Set ylims with two ranges
Usingylims=((0, 3), (50, 55))creates a broken y-axis showing both clusters clearly.Final Answer:
Use ylims=((0, 3), (50, 55)) when creating brokenaxes -> Option BQuick Check:
Broken y-axis = ylims tuple of ranges [OK]
- Breaking x-axis instead of y-axis
- Using single continuous ylims hiding gap
- Plotting separate figures unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
