Challenge - 5 Problems
Shared Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of shared x-axis subplots
What will be the output of this code that creates two subplots sharing the x-axis?
Matplotlib
import matplotlib.pyplot as plt fig, (ax1, ax2) = plt.subplots(2, sharex=True) ax1.plot([1, 2, 3], [1, 4, 9]) ax2.plot([1, 2, 3], [2, 3, 4]) plt.show()
Attempts:
2 left
💡 Hint
Think about what sharex=True does when creating subplots stacked vertically.
✗ Incorrect
Using sharex=True makes the subplots share the same x-axis. When stacked vertically, only the bottom plot shows the x-axis labels to avoid repetition.
❓ data_output
intermediate2:00remaining
Number of tick labels shown with shared y-axis
Given this code, how many y-axis tick labels will be visible in total?
Matplotlib
import matplotlib.pyplot as plt fig, (ax1, ax2) = plt.subplots(2, sharey=True) ax1.plot([1, 2, 3], [1, 4, 9]) ax2.plot([1, 2, 3], [2, 3, 4]) plt.show()
Attempts:
2 left
💡 Hint
When subplots are stacked vertically with sharey=True, both show y-axis tick labels on the left side, as hiding applies to horizontal sharing.
✗ Incorrect
With sharey=True in a vertical stack (2 rows, 1 column), both subplots display y-axis tick labels on their left side because automatic hiding of y-labels occurs only for horizontally adjacent subplots.
❓ visualization
advanced2:30remaining
Effect of sharex and sharey on subplot grid
Which option correctly describes the layout and axis sharing of this code?
Matplotlib
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2, sharex='col', sharey='row') for i in range(2): for j in range(2): axs[i, j].plot([1, 2, 3], [1, 2, 3]) plt.show()
Attempts:
2 left
💡 Hint
Check the parameters sharex='col' and sharey='row' carefully.
✗ Incorrect
sharex='col' means subplots in the same column share x-axis; sharey='row' means subplots in the same row share y-axis.
🔧 Debug
advanced2:00remaining
Identify the error in shared axes subplot code
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2, sharex=True, sharey=True) axs[0].plot([1, 2, 3], [1, 4, 9]) plt.show()
Attempts:
2 left
💡 Hint
Check how axs is indexed when subplots creates a 2x2 array.
✗ Incorrect
plt.subplots(2, 2) returns a 2D array of axes. axs[0] is a 1D array (the first row), not a single AxesSubplot, so axs[0].plot(...) causes AttributeError: 'numpy.ndarray' object has no attribute 'plot'.
🚀 Application
expert3:00remaining
Creating a complex shared axes layout
You want to create a 3x1 subplot layout where all subplots share the x-axis but have independent y-axes. Which code snippet achieves this correctly?
Attempts:
2 left
💡 Hint
Sharing x-axis means sharex=True; independent y-axes means no sharey.
✗ Incorrect
Setting sharex=True shares the x-axis among subplots. Not setting sharey means y-axes are independent.