Bird
Raised Fist0
Matplotlibdata~10 mins

Broken axes concept in Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Broken axes concept
Start: Prepare data
↓
Create figure and axes
↓
Plot data on first axis
↓
Plot data on second axis
↓
Hide spines and ticks between axes
↓
Add diagonal lines to show break
↓
Show combined plot with broken axes
↓
End
This flow shows how to create a plot with broken axes by preparing data, plotting on two axes, hiding the break area, and adding diagonal lines to indicate the break.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot([1, 2, 3], [10, 20, 30])
ax2.plot([1, 2, 3], [100, 200, 300])
plt.show()
This code creates two side-by-side plots sharing the y-axis to simulate broken axes.
Execution Table
StepActionAxes StatePlot DataVisual Result
1Import matplotlib.pyplot as pltNo axes createdNo data plottedNo plot yet
2Create figure and two subplots sharing y-axisTwo axes: ax1 and ax2 created side by sideNo data plottedEmpty figure with two axes
3Plot data [10,20,30] on ax1ax1 readyLine plot on ax1 with y=[10,20,30]Left plot shows small y values
4Plot data [100,200,300] on ax2ax2 readyLine plot on ax2 with y=[100,200,300]Right plot shows large y values
5Hide spines and ticks between ax1 and ax2Axes spines adjustedNo new dataGap between plots looks like broken axis
6Add diagonal lines on breakDecorations addedNo new dataVisual break lines appear between axes
7Show plotFinal stateBoth plots visibleCombined plot with broken y-axis shown
8EndPlot window openData displayedUser sees broken axes plot
💡 Plot displayed with two axes showing different y ranges to simulate broken axes.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
figNoneFigure object createdFigure unchangedFigure unchangedFigure unchangedFigure unchangedFigure unchanged
ax1NoneAxes object createdLine plotted with y=[10,20,30]Line plottedSpines hidden on rightDiagonal lines addedFinal broken axes left plot
ax2NoneAxes object createdNo plotLine plotted with y=[100,200,300]Spines hidden on leftDiagonal lines addedFinal broken axes right plot
Key Moments - 3 Insights
Why do we create two separate axes instead of one?
We create two axes to show different y-axis ranges side by side, which simulates a broken axis effect. This is shown in execution_table steps 2, 3, and 4 where each axis plots different y data.
How do diagonal lines indicate the axis break?
Diagonal lines are drawn between the two axes to visually signal the break in the axis scale. This is done after hiding spines (step 6) and helps users understand the axis is discontinuous.
Why do we share the y-axis between the two plots?
Sharing the y-axis aligns the two plots vertically so the broken axis looks continuous except for the break. This is set in step 2 with sharey=True.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What data is plotted on ax2?
A[10, 20, 30]
B[100, 200, 300]
C[1, 2, 3]
DNo data plotted yet
💡 Hint
Check the 'Plot Data' column in row for step 4 in execution_table.
At which step do we add diagonal lines to show the axis break?
AStep 6
BStep 5
CStep 3
DStep 7
💡 Hint
Look at the 'Action' column for the step mentioning diagonal lines in execution_table.
If we do not share the y-axis (remove sharey=True), what changes in the plot?
APlots will merge into one
BDiagonal lines will not appear
CAxes will not align vertically, making the broken axis effect less clear
DData will not plot
💡 Hint
Refer to key_moments about why sharey=True is used.
Concept Snapshot
Broken axes concept in matplotlib:
- Create two subplots side by side with sharey=True
- Plot different y ranges on each axis
- Hide spines and ticks between axes
- Add diagonal lines to indicate axis break
- Show combined plot simulating a broken axis
Full Transcript
This visual execution shows how to create a broken axes plot in matplotlib. First, we import matplotlib and create a figure with two subplots side by side sharing the y-axis. We plot smaller y values on the left axis and larger y values on the right axis. Then, we hide the spines and ticks between the two axes to create a visual gap. Diagonal lines are added between the axes to indicate the break in the axis scale. Finally, the combined plot is shown, allowing viewers to see data with a discontinuous y-axis. This technique helps display data with very different scales clearly.

Practice

(1/5)
1. What is the main purpose of using broken axes in matplotlib plots?
easy
A. To skip unimportant data ranges and focus on key parts
B. To add 3D effects to the plot
C. To change the color scheme automatically
D. To animate the plot over time

Solution

  1. 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.
  2. Step 2: Identify the main purpose

    This helps to focus on important data ranges without showing the entire axis continuously.
  3. Final Answer:

    To skip unimportant data ranges and focus on key parts -> Option A
  4. Quick Check:

    Broken axes = skip gaps [OK]
Hint: Broken axes hide gaps to highlight important data [OK]
Common Mistakes:
  • Thinking broken axes add 3D or animation
  • Confusing broken axes with color changes
  • Assuming broken axes zoom in automatically
2. Which of the following is the correct way to import the brokenaxes package to use brokenaxes(...) directly for creating broken axes plots?
easy
A. from brokenaxes import BrokenAxes
B. import brokenaxes as ba
C. from brokenaxes import brokenaxes
D. import matplotlib.brokenaxes as ba

Solution

  1. Step 1: Recall the correct import syntax

    The brokenaxes package provides the brokenaxes function to create broken axes plots.
  2. Step 2: Identify the correct import statement

    The correct way is from brokenaxes import brokenaxes to access the function directly.
  3. Final Answer:

    from brokenaxes import brokenaxes -> Option C
  4. Quick Check:

    Import brokenaxes function = from brokenaxes import brokenaxes [OK]
Hint: Import brokenaxes function directly from brokenaxes [OK]
Common Mistakes:
  • Using capital 'BrokenAxes' instead of 'brokenaxes'
  • Trying to import from matplotlib instead of brokenaxes
  • Using wrong import syntax causing ImportError
3. What will the following code output?
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()
medium
A. A plot with y-axis broken between 2 and 8 showing points near 1 and 9
B. A plot with continuous y-axis from 0 to 10
C. An error because ylims must be a single tuple
D. A plot with x-axis broken between 2 and 8

Solution

  1. Step 1: Analyze the ylims parameter

    The ylims=((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.
  2. 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.
  3. Final Answer:

    A plot with y-axis broken between 2 and 8 showing points near 1 and 9 -> Option A
  4. Quick Check:

    ylims tuple of ranges = broken y-axis [OK]
Hint: ylims with tuples creates breaks on y-axis [OK]
Common Mistakes:
  • Assuming y-axis is continuous without breaks
  • Confusing x-axis and y-axis breaks
  • Expecting an error from tuple ylims
4. Identify the error in this code snippet that tries to create broken axes:
from brokenaxes import brokenaxes
import matplotlib.pyplot as plt

bax = brokenaxes(xlims=(0, 2), (5, 7))
bax.plot([1, 6], [10, 20])
plt.show()
medium
A. plt.show() must be called before plot
B. plot function requires x and y to be the same length
C. BrokenAxes cannot be used with xlims parameter
D. xlims should be a tuple of tuples, not separate tuples

Solution

  1. Step 1: Check the xlims argument syntax

    The code uses xlims=(0, 2), (5, 7) which is invalid syntax; it should be a single tuple containing tuples like xlims=((0, 2), (5, 7)).
  2. Step 2: Confirm other parts are correct

    The plot call and plt.show() are correct; the error is only in xlims syntax.
  3. Final Answer:

    xlims should be a tuple of tuples, not separate tuples -> Option D
  4. Quick Check:

    xlims syntax = tuple of tuples [OK]
Hint: Use double parentheses for xlims tuple of ranges [OK]
Common Mistakes:
  • Passing multiple tuples without enclosing in one tuple
  • Mixing up xlims and ylims syntax
  • Calling plt.show() too early
5. You have data with y-values clustered around 0-3 and 50-55, with a large gap in between. How would you create a broken y-axis to show both clusters clearly using brokenaxes?
hard
A. Plot two separate figures for each cluster
B. Use ylims=((0, 3), (50, 55)) when creating brokenaxes
C. Use xlims=((0, 3), (50, 55)) to break x-axis instead
D. Use ylims=(0, 55) to show full range without breaks

Solution

  1. 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.
  2. Step 2: Set ylims with two ranges

    Using ylims=((0, 3), (50, 55)) creates a broken y-axis showing both clusters clearly.
  3. Final Answer:

    Use ylims=((0, 3), (50, 55)) when creating brokenaxes -> Option B
  4. Quick Check:

    Broken y-axis = ylims tuple of ranges [OK]
Hint: Set ylims with two ranges to break y-axis gaps [OK]
Common Mistakes:
  • Breaking x-axis instead of y-axis
  • Using single continuous ylims hiding gap
  • Plotting separate figures unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard