Skip to content

Commit 52cb034

Browse files
authored
Merge pull request #31756 from meeseeksmachine/auto-backport-of-pr-31720-on-v3.11.x
Backport PR #31720 on branch v3.11.x (DOC: Add axis sharing section to subplot_mosaic guide)
2 parents cb03a5c + d4ca17d commit 52cb034

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

galleries/users_explain/axes/mosaic.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,56 @@ def identify_axes(ax_dict, fontsize=48):
390390
empty_sentinel=0,
391391
)
392392
identify_axes(axd)
393+
394+
395+
# %%
396+
# Axis sharing
397+
# ============
398+
#
399+
# `.Figure.subplot_mosaic` supports *sharex* and *sharey* as does
400+
# `.Figure.subplots`. When ``True``, all Axes in the mosaic share
401+
# the same x-axis or y-axis, which is useful for directly comparing
402+
# data across panels.
403+
404+
fig = plt.figure(layout="constrained")
405+
axd = fig.subplot_mosaic("AC;BC", sharex=True)
406+
axd["A"].set_title("Axes A")
407+
axd["B"].set_title("Axes B")
408+
axd["C"].set_title("Axes C")
409+
for ax in axd.values():
410+
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
411+
identify_axes(axd)
412+
413+
# %%
414+
# Note that when using ``sharex=True`` only the bottom row of Axes shows
415+
# x-axis tick labels, and when using ``sharey=True`` only the left column
416+
# shows y-axis tick labels. This is identical to the behavior of
417+
# `.Figure.subplots`.
418+
#
419+
# Interaction with empty sentinels
420+
# ---------------------------------
421+
#
422+
# When the mosaic layout has blank spaces (empty sentinels), axis sharing
423+
# can produce inintended results. In the following example, there are no
424+
# tick labels in the top row:
425+
426+
fig, axd = plt.subplot_mosaic(".A;BC", sharey=True, layout="constrained")
427+
for ax in axd.values():
428+
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
429+
identify_axes(axd)
430+
431+
# %%
432+
# The reason for this behavior is that sharing removes tick labels from
433+
# all but the most-left / most-bottom grid positions.
434+
#
435+
# The fix is to manually re-enable tick labels on the Axes that should
436+
# display them using `.Axes.tick_params`:
437+
438+
fig, axd = plt.subplot_mosaic(".A;BC", sharey=True, layout="constrained")
439+
for ax in axd.values():
440+
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
441+
442+
# Re-enable y-axis tick labels on Axes "A"
443+
axd["A"].tick_params(labelleft=True)
444+
445+
identify_axes(axd)

0 commit comments

Comments
 (0)