Bird
Raised Fist0
Matplotlibdata~15 mins

Broken axes concept in Matplotlib - Deep Dive

Choose your learning style10 modes available
Overview - Broken axes concept
What is it?
Broken axes is a way to show parts of a plot that are far apart in value by splitting the axis into sections. This helps display data with large gaps or outliers clearly without losing detail in other parts. Instead of one continuous axis, the axis is 'broken' to zoom in on important ranges. This makes graphs easier to read and understand when data values vary widely.
Why it matters
Without broken axes, plots with very different value ranges can look squished or hide important details. For example, if one data point is very large, the rest may appear flat and unreadable. Broken axes let you focus on multiple value ranges in one plot, making it easier to compare and analyze data. This improves communication and decision-making based on visual data.
Where it fits
Learners should know basic plotting with matplotlib and understand axes and scales. After mastering broken axes, they can explore advanced visualization techniques like subplots, inset plots, and interactive zooming. Broken axes is a useful tool in the data visualization journey to handle tricky data distributions.
Mental Model
Core Idea
Broken axes split a plot's axis into separate segments to zoom in on different value ranges, making wide-ranging data clear and comparable.
Think of it like...
Imagine a ruler with a gap in the middle so you can focus on the start and end parts without showing the empty space in between. This lets you measure small and large lengths clearly on the same ruler.
┌─────────────┐
│  Data Plot  │
├─────┬───────┤
│ 0-10│ 90-100│  <-- Axis broken between 10 and 90
└─────┴───────┘
Build-Up - 6 Steps
1
FoundationUnderstanding basic matplotlib axes
🤔
Concept: Learn what axes are and how matplotlib uses them to plot data.
In matplotlib, an axis is the line with ticks and labels that shows the scale of data. The x-axis is horizontal, and the y-axis is vertical. When you plot data, matplotlib draws points or lines according to these axes. For example, plt.plot([1,2,3], [4,5,6]) plots points with x values 1,2,3 and y values 4,5,6.
Result
A simple line plot with continuous x and y axes.
Understanding axes is key because broken axes modify these lines to show data differently.
2
FoundationRecognizing axis scale challenges
🤔
Concept: See why normal axes struggle with data having large gaps or outliers.
If your data has values like 1, 2, 3 and also 1000, plotting on a normal axis makes the small values look flat near zero. The large value stretches the axis, hiding details. This is a common problem when data ranges vary widely.
Result
A plot where small values cluster and large values dominate the scale.
Knowing this problem motivates the need for broken axes to improve visualization.
3
IntermediateCreating broken axes with matplotlib tools
🤔Before reading on: do you think broken axes require special plotting libraries or can matplotlib handle it?
Concept: Learn how to use matplotlib's subplots and axis hiding to create broken axes effect.
Matplotlib does not have a built-in broken axes function, but you can create two subplots stacked or side-by-side with shared axes. Hide the spines and ticks between them and add diagonal lines to show the break. This tricks the eye into seeing one plot with a broken axis.
Result
Two aligned plots showing different y ranges with a visual break between them.
Knowing how to combine subplots and customize axes lets you build broken axes without extra libraries.
4
IntermediateUsing the brokenaxes library for simplicity
🤔Before reading on: do you think a third-party library can simplify broken axes creation?
Concept: Discover the brokenaxes Python library that automates broken axes plotting.
The brokenaxes library wraps matplotlib and lets you define axis breaks easily. You specify ranges to skip, and it handles subplot creation, axis hiding, and break marks. For example: from brokenaxes import brokenaxes bax = brokenaxes(ylims=((0,10),(90,100))) bax.plot(x, y) This creates a y-axis broken between 10 and 90.
Result
A clean plot with broken y-axis showing two separate ranges seamlessly.
Using specialized libraries saves time and reduces errors in complex visualizations.
5
AdvancedCustomizing broken axes appearance
🤔Before reading on: do you think broken axes always look the same or can you style them?
Concept: Learn how to style breaks, ticks, and labels for clarity and aesthetics.
You can customize the diagonal break marks, colors, tick positions, and labels to make broken axes clearer. For example, adding diagonal lines with plt.plot or using patches to draw break marks. Adjusting tick labels to avoid confusion is important. This improves readability and professionalism.
Result
A visually appealing broken axes plot that clearly shows axis breaks.
Styling broken axes enhances communication and prevents misinterpretation.
6
ExpertHandling multiple axis breaks and interactions
🤔Before reading on: do you think broken axes can handle multiple breaks or interactive zooming easily?
Concept: Explore advanced use cases with multiple breaks and integrating broken axes with interactive tools.
You can create plots with several broken axis segments by stacking multiple subplots or using brokenaxes with multiple limits. Combining broken axes with interactive zoom or pan requires careful synchronization of axes and event handling. This is complex but powerful for detailed data exploration.
Result
Complex plots showing multiple value ranges with interactive features.
Mastering these techniques enables professional-grade visualizations for complex datasets.
Under the Hood
Broken axes work by splitting the plotting area into multiple subplots that share an axis but display different value ranges. The axis lines and ticks are selectively hidden or modified to create the illusion of a continuous axis with gaps. The plotting library draws each subplot independently but aligns them visually. This requires careful control of axis limits, tick marks, and decorations to avoid confusing the viewer.
Why designed this way?
This approach was chosen because plotting libraries like matplotlib are built around continuous axes. Instead of redesigning the entire axis system, using multiple aligned subplots is simpler and leverages existing features. It balances flexibility and complexity, allowing users to customize breaks without deep changes to the plotting engine.
┌─────────────┐
│  Plot Area  │
├─────┬───────┤
│Sub1 │ Sub2  │
│(0-10) (90-100)│
├─────┴───────┤
│ Shared X-axis│
└─────────────┘
Myth Busters - 3 Common Misconceptions
Quick: Do broken axes change the actual data values or just how they are shown? Commit yes or no.
Common Belief:Broken axes distort data values to fit them into the plot.
Tap to reveal reality
Reality:Broken axes do not change data values; they only change how the axis scale is displayed by splitting it.
Why it matters:Believing data is changed can lead to mistrust in visualizations and wrong conclusions.
Quick: Can broken axes be created with a single continuous axis in matplotlib? Commit yes or no.
Common Belief:You can create broken axes with one continuous axis line in matplotlib.
Tap to reveal reality
Reality:Matplotlib requires multiple subplots or special libraries to simulate broken axes; a single continuous axis cannot have gaps.
Why it matters:Trying to force a single axis to break can cause confusing or incorrect plots.
Quick: Do broken axes always improve plot readability? Commit yes or no.
Common Belief:Broken axes always make plots easier to understand.
Tap to reveal reality
Reality:Broken axes can confuse viewers if not clearly marked or overused, making interpretation harder.
Why it matters:Misusing broken axes can reduce clarity and mislead the audience.
Expert Zone
1
Broken axes require careful alignment of subplots to avoid visual discontinuities that confuse viewers.
2
Tick labels and grid lines must be managed precisely to prevent misinterpretation of scale breaks.
3
Interactive plots with broken axes need synchronization of zoom and pan events across subplots, which is non-trivial.
When NOT to use
Avoid broken axes when data can be transformed (e.g., log scale) to show wide ranges without breaks. Also, do not use broken axes if the breaks might mislead or confuse the audience; consider inset plots or interactive zoom instead.
Production Patterns
Professionals use broken axes in scientific papers to highlight small and large value ranges simultaneously. They combine broken axes with annotations and legends to explain breaks. In dashboards, broken axes are less common due to interactivity but appear in static reports.
Connections
Logarithmic scale
Alternative approach to handle wide data ranges
Understanding broken axes helps appreciate when log scales are better or worse for visualizing data with large value differences.
Data transformation
Preprocessing step to reduce data range before plotting
Knowing broken axes clarifies why transforming data (e.g., normalization) can sometimes replace the need for axis breaks.
User interface design
Visual communication technique to manage complex information
Broken axes relate to UI patterns that segment information to avoid overload, showing how visualization principles cross domains.
Common Pitfalls
#1Plotting broken axes without clear break marks
Wrong approach:plt.subplot(211) plt.plot(x, y) plt.subplot(212) plt.plot(x, y) # No break marks or axis hiding
Correct approach:Use diagonal lines or zigzag marks between subplots and hide overlapping spines to indicate breaks clearly.
Root cause:Not signaling axis breaks confuses viewers who may think the axis is continuous.
#2Using broken axes for data that fits well on a log scale
Wrong approach:Creating broken axes for data ranging from 1 to 1000 without trying log scale.
Correct approach:Apply plt.yscale('log') to show data on a logarithmic scale instead of breaking axes.
Root cause:Not considering simpler transformations leads to unnecessary complexity.
#3Misaligning subplots causing axis ticks to mismatch
Wrong approach:Creating subplots with different widths or misaligned x-axes causing visual gaps.
Correct approach:Use shared axes and tight layout to align subplots perfectly.
Root cause:Ignoring subplot alignment breaks the illusion of a single plot.
Key Takeaways
Broken axes split a plot's axis into segments to show data ranges separately but in one figure.
They solve the problem of visualizing data with large gaps or outliers without losing detail.
Matplotlib achieves broken axes by combining multiple aligned subplots with hidden spines and break marks.
Specialized libraries like brokenaxes simplify creating broken axes plots.
Careful styling and clear break indicators are essential to avoid confusing viewers.

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