📖 Scenario: Imagine you are analyzing sales data for a store. The sales numbers vary a lot: some days have very low sales, and some days have very high sales. You want to create a chart that shows both low and high sales clearly without wasting space.
🎯 Goal: You will create a plot with broken axes using matplotlib to show sales data with a gap in the y-axis. This helps to focus on both small and large sales values in one chart.
📋 What You'll Learn
Create a list called days with values from 1 to 10.
Create a list called sales with these exact values: 5, 7, 6, 100, 110, 105, 8, 6, 7, 5.
Create a figure with two subplots stacked vertically sharing the x-axis.
Set the y-axis limits of the top subplot to show high sales (90 to 120).
Set the y-axis limits of the bottom subplot to show low sales (0 to 10).
Plot the sales data on both subplots using ax.plot(days, sales).
Hide the spines between the two subplots to show the break clearly.
Add diagonal lines to indicate the break in the y-axis.
Print the figure to display the broken axes plot.
💡 Why This Matters
🌍 Real World
Broken axes plots are useful when data has large gaps or outliers, such as sales data with occasional spikes, to show details clearly without wasting space.
💼 Career
Data scientists and analysts often use broken axes plots to communicate insights effectively when data ranges vary widely.
Progress0 / 4 steps
1
Create the sales data lists
Create a list called days with values from 1 to 10, and a list called sales with these exact values: 5, 7, 6, 100, 110, 105, 8, 6, 7, 5.
Matplotlib
Hint
Use square brackets to create lists. For days, list numbers 1 through 10. For sales, use the exact numbers given.
2
Create the figure and subplots with shared x-axis
Import matplotlib.pyplot as plt. Then create a figure and two subplots stacked vertically using plt.subplots(2, 1, sharex=True). Assign the subplots to variables ax1 and ax2.
Matplotlib
Hint
Use plt.subplots(2, 1, sharex=True) to create two vertical plots sharing the x-axis.
3
Set y-axis limits and plot sales data
Set the y-axis limits of ax1 to 90 to 120, and ax2 to 0 to 10 using set_ylim(). Then plot the sales data on both subplots using ax1.plot(days, sales) and ax2.plot(days, sales).
Matplotlib
Hint
Use set_ylim() to set y-axis limits. Use plot() to draw the sales line on both axes.
4
Hide spines and add diagonal break lines, then show plot
Hide the bottom spine of ax1 and the top spine of ax2 by setting their visibility to False. Add diagonal lines on both subplots to indicate the broken axis using ax1.plot() and ax2.plot() with coordinates [(0.98, 0.02), (1.02, -0.02)] in axes fraction coordinates. Finally, call plt.show() to display the plot.
Matplotlib
Hint
Use spines['bottom'].set_visible(False) and spines['top'].set_visible(False) to hide spines. Use plot() with axes fraction coordinates to draw diagonal lines. Finally, call plt.show() to display the figure.
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
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 A
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
Step 1: Recall the correct import syntax
The brokenaxes package provides the brokenaxes function to create broken axes plots.
Step 2: Identify the correct import statement
The correct way is from brokenaxes import brokenaxes to access the function directly.
Final Answer:
from brokenaxes import brokenaxes -> Option C
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
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
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.
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 A
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
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)).
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 D
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
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
Using ylims=((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 B
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 unknownstrongwhile earning +1 Grip toward your certificate.