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
Step 1: Understand sharex and sharey effect on limits
Sharing axes means setting limits on one subplot updates all linked subplots.
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.
Final Answer:
(0.0, 3.0) because all x-axes are shared and limits set on one affect all. -> Option C
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
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.
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.
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.
Final Answer:
Use plt.subplots(2, 2, sharex='row', sharey='col') to share x in rows and y in columns. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.