Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of using sharex or sharey when creating subplots in matplotlib?
easy
A. It changes the color of the axes to match across subplots.
B. It makes the x or y axes use the same scale across subplots for easier comparison.
C. It automatically adds titles to each subplot.
D. It increases the size of the figure window.

Solution

  1. Step 1: Understand the purpose of shared axes

    Shared axes mean multiple plots use the same scale on x or y axis, making it easier to compare data visually.
  2. Step 2: Identify what sharex and sharey do

    These parameters in plt.subplots() link the axes scales so they stay consistent across subplots.
  3. Final Answer:

    It makes the x or y axes use the same scale across subplots for easier comparison. -> Option B
  4. Quick Check:

    Shared axes = same scale for easy comparison [OK]
Hint: Shared axes keep scales same for easy visual comparison [OK]
Common Mistakes:
  • Thinking shared axes change colors
  • Assuming shared axes add titles automatically
  • Believing shared axes affect figure size
2. Which of the following is the correct way to create two subplots sharing the y-axis using matplotlib?
easy
A. plt.subplots(1, 2, sharex=false)
B. plt.subplots(2, 1, sharex=true)
C. plt.subplots(1, 2, sharey=false)
D. plt.subplots(2, 1, sharey=true)

Solution

  1. Step 1: Identify the parameter for sharing y-axis

    The parameter sharey=true shares the y-axis between subplots.
  2. Step 2: Check the subplot layout

    2, 1 means 2 rows and 1 column, which is valid for vertical stacking with shared y-axis.
  3. Final Answer:

    plt.subplots(2, 1, sharey=true) -> Option D
  4. Quick Check:

    sharey=true shares y-axis [OK]
Hint: Use sharey=true for vertical subplots sharing y-axis [OK]
Common Mistakes:
  • Using sharex instead of sharey
  • Mixing rows and columns incorrectly
  • Setting sharey=false when sharing is needed
3. What will be the output of the following code snippet?
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2, sharex=True)
axs[0].plot([1, 2, 3], [10, 20, 30])
axs[1].plot([1, 2, 3], [15, 25, 35])
print(axs[1].get_xlim())
medium
A. (0.9, 3.1)
B. (10.0, 35.0)
C. (1.0, 3.0)
D. (15.0, 35.0)

Solution

  1. Step 1: Understand sharex=true effect

    With sharex=True, both subplots share the same x-axis limits.
  2. Step 2: Check data x-values and default limits

    The x data is [1, 2, 3]. Matplotlib adds a small margin, so limits are slightly beyond 1 and 3, e.g., (0.9, 3.1).
  3. Final Answer:

    (0.9, 3.1) -> Option A
  4. Quick Check:

    Shared x-axis limits include margin around data [OK]
Hint: Shared axes use same limits with small margin [OK]
Common Mistakes:
  • Confusing x and y limits
  • Expecting exact data min/max as limits
  • Ignoring margin matplotlib adds
4. Consider this code:
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
axs[0, 0].plot([1, 2], [3, 4])
axs[1, 1].plot([1, 2], [5, 6])
axs[1, 0].set_xlim(0, 3)
print(axs[0, 1].get_xlim())

What is the likely output and why might it cause confusion?
medium
A. Error because set_xlim cannot be used with shared axes.
B. (1.0, 2.0) because each subplot keeps its own limits despite sharing.
C. (0.0, 3.0) because all x-axes are shared and limits set on one affect all.
D. (3.0, 0.0) because limits are reversed causing an error.

Solution

  1. Step 1: Understand sharex and sharey effect on limits

    Sharing axes means setting limits on one subplot updates all linked subplots.
  2. Step 2: Analyze set_xlim call and its effect

    Setting set_xlim(0, 3) on axs[1, 0] changes x-axis limits for all subplots sharing x-axis.
  3. Final Answer:

    (0.0, 3.0) because all x-axes are shared and limits set on one affect all. -> Option C
  4. Quick Check:

    Shared axes share limits changes [OK]
Hint: Changing limits on one shared axis updates all linked subplots [OK]
Common Mistakes:
  • Thinking each subplot keeps independent limits
  • Expecting error from set_xlim with shared axes
  • Confusing reversed limits as error
5. You want to create a 2x2 grid of subplots where the top row shares the x-axis, the left column shares the y-axis, but the bottom row and right column do not share axes. Which code snippet correctly achieves this?
hard
A. Use plt.subplots(2, 2, sharex='row', sharey='col') to share x in rows and y in columns.
B. Create subplots with sharex='col' and sharey='row' then manually adjust axes sharing.
C. Use plt.subplots(2, 2, sharex='col', sharey='row') to share x in columns and y in rows.
D. Create subplots without sharing and link axes manually with ax.sharex() and ax.sharey().

Solution

  1. Step 1: Understand sharex and sharey options

    In plt.subplots(), sharex='col' shares x-axis within each column, and sharey='row' shares y-axis within each row.
  2. Step 2: Match requirement to parameters

    We want top row to share x-axis (same x in row), so sharex='row' is needed. Left column shares y-axis (same y in column), so sharey='col' is needed.
  3. Step 3: Identify correct code

    Use plt.subplots(2, 2, sharex='col', sharey='row') to share x in columns and y in rows. uses sharex='col' and sharey='row', which is reversed. Use plt.subplots(2, 2, sharex='row', sharey='col') to share x in rows and y in columns. uses sharex='row' and sharey='col', which matches the requirement.
  4. Final Answer:

    Use plt.subplots(2, 2, sharex='row', sharey='col') to share x in rows and y in columns. -> Option A
  5. Quick Check:

    sharex='row' and sharey='col' matches sharing needs [OK]
Hint: sharex='row' shares x in rows; sharey='col' shares y in columns [OK]
Common Mistakes:
  • Confusing 'row' and 'col' values for sharex/sharey
  • Assuming sharex='col' shares x in rows
  • Trying to manually link axes 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