Skip to content

Commit e402c91

Browse files
author
Atharva2012
committed
DOC: Add axis sharing section to subplot_mosaic guide
1 parent 39c556c commit e402c91

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

galleries/users_explain/axes/mosaic.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,59 @@ 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 unexpected results. In the following example, the top-left
424+
# cell is empty and ``sharey=True`` is used:
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+
# Because the left column's topmost Axes is blank, Axes ``"B"`` would
433+
# normally be the leftmost visible Axes in its row, but axis sharing has
434+
# already removed its y-axis tick labels (since it is not the first column
435+
# in the grid). As a result, *none* of the Axes in the top row display
436+
# y-axis tick labels, which is usually not what you want.
437+
#
438+
# The fix is to manually re-enable tick labels on the Axes that should
439+
# display them using `.Axes.tick_params`:
440+
441+
fig, axd = plt.subplot_mosaic(".A;BC", sharey=True, layout="constrained")
442+
for ax in axd.values():
443+
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
444+
445+
# Re-enable y-axis tick labels on Axes "B"
446+
axd["B"].tick_params(labelleft=True)
447+
448+
identify_axes(axd)

0 commit comments

Comments
 (0)