0
0
Matplotlibdata~30 mins

Broken axes concept in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Broken axes concept
📖 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
Need a 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
Need a 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
Need a 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
Need a 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.