0
0
Matplotlibdata~20 mins

Shared axes between subplots in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shared Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AA single plot with two lines overlapping sharing both axes.
BTwo plots stacked vertically with independent x-axis labels on both plots.
CTwo plots stacked vertically with the same x-axis labels visible only on the bottom plot.
DTwo plots side by side sharing the y-axis with independent x-axis labels.
Attempts:
2 left
💡 Hint
Think about what sharex=True does when creating subplots stacked vertically.
data_output
intermediate
2: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()
ANo y-axis tick labels are shown on either subplot.
BBoth subplots show independent y-axis tick labels on their left side.
COnly the left y-axis of the first subplot shows tick labels; the second subplot hides them.
DBoth subplots show y-axis tick labels on the right side.
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.
visualization
advanced
2: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()
ASubplots in each row share the x-axis; subplots in each column share the y-axis.
BAll subplots share both x and y axes globally.
CNo axes are shared; each subplot has independent axes.
DSubplots in each column share the x-axis; subplots in each row share the y-axis.
Attempts:
2 left
💡 Hint
Check the parameters sharex='col' and sharey='row' carefully.
🔧 Debug
advanced
2: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()
AAttributeError: 'numpy.ndarray' object has no attribute 'plot'
BIndexError: list index out of range
CAttributeError: 'Figure' object has no attribute 'plot'
DNo error; the code runs and shows plots.
Attempts:
2 left
💡 Hint
Check how axs is indexed when subplots creates a 2x2 array.
🚀 Application
expert
3: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?
A
fig, axs = plt.subplots(3, 1, sharex=True)
for ax in axs:
    ax.plot([1, 2, 3], [1, 2, 3])
plt.show()
B
fig, axs = plt.subplots(3, 1, sharey=True)
for ax in axs:
    ax.plot([1, 2, 3], [1, 2, 3])
plt.show()
C
fig, axs = plt.subplots(3, 1, sharex=False)
for ax in axs:
    ax.plot([1, 2, 3], [1, 2, 3])
plt.show()
D
fig, axs = plt.subplots(3, 1)
for ax in axs:
    ax.plot([1, 2, 3], [1, 2, 3])
plt.show()
Attempts:
2 left
💡 Hint
Sharing x-axis means sharex=True; independent y-axes means no sharey.