Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0327cfb

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR #20147: DOC: add example of labelling axes
1 parent 26cb8af commit 0327cfb

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
==================
3+
Labelling subplots
4+
==================
5+
6+
Labelling subplots is relatively straightforward, and varies,
7+
so Matplotlib does not have a general method for doing this.
8+
9+
Simplest is putting the label inside the axes. Note, here
10+
we use `.pyplot.subplot_mosaic`, and use the subplot labels
11+
as keys for the subplots, which is a nice convenience. However,
12+
the same method works with `.pyplot.subplots` or keys that are
13+
different than what you want to label the subplot with.
14+
"""
15+
16+
import matplotlib.pyplot as plt
17+
import matplotlib.transforms as mtransforms
18+
19+
fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],
20+
constrained_layout=True)
21+
22+
for label, ax in axs.items():
23+
# label physical distance in and down:
24+
trans = mtransforms.ScaledTranslation(10/72, -5/72, fig.dpi_scale_trans)
25+
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
26+
fontsize='medium', verticalalignment='top', fontfamily='serif',
27+
bbox=dict(facecolor='0.7', edgecolor='none', pad=3.0))
28+
29+
plt.show()
30+
31+
##############################################################################
32+
# We may prefer the labels outside the axes, but still aligned
33+
# with each other, in which case we use a slightly different transform:
34+
35+
fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],
36+
constrained_layout=True)
37+
38+
for label, ax in axs.items():
39+
# label physical distance to the left and up:
40+
trans = mtransforms.ScaledTranslation(-20/72, 7/72, fig.dpi_scale_trans)
41+
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
42+
fontsize='medium', va='bottom', fontfamily='serif')
43+
44+
plt.show()
45+
46+
##############################################################################
47+
# If we want it aligned with the title, either incorporate in the title or
48+
# use the *loc* keyword argument:
49+
50+
fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],
51+
constrained_layout=True)
52+
53+
for label, ax in axs.items():
54+
ax.set_title('Normal Title', fontstyle='italic')
55+
ax.set_title(label, fontfamily='serif', loc='left', fontsize='medium')
56+
57+
plt.show()
58+
59+
#############################################################################
60+
#
61+
# .. admonition:: References
62+
#
63+
# The use of the following functions, methods, classes and modules is shown
64+
# in this example:
65+
#
66+
# - `matplotlib.figure.Figure.subplot_mosaic` /
67+
# `matplotlib.pyplot.subplot_mosaic`
68+
# - `matplotlib.axes.Axes.set_title`
69+
# - `matplotlib.axes.Axes.text`
70+
# - `matplotlib.transforms.ScaledTranslation`

0 commit comments

Comments
 (0)