Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb7cdf77

Browse files
committed
Use annotate coordinate systems to simplify label_subplots.
The ability of annotate to specify various coordinates systemssimplifies label_subplots, by avoiding the need to explicitly constructa ScaledTranslation (while maintaining exact functionality parity).
1 parentc5707d9 commitb7cdf77

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

‎galleries/examples/text_labels_and_annotations/label_subplots.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,57 @@
33
Labelling subplots
44
==================
55
6-
Labelling subplots is relatively straightforward, and varies,
7-
so Matplotlibdoes not have a general method for doing this.
6+
Labelling subplots is relatively straightforward, and varies, so Matplotlib
7+
does not have a general method for doing this.
88
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.
9+
The examples here rely on the ability of `~.Axes.annotate` to conveniently
10+
position text at a given physical offset (in fontsize units or in points) away
11+
from a corner of the Axes.
12+
13+
Note, here we use `.pyplot.subplot_mosaic`, and use the subplot labels as keys
14+
for the subplots, which is a nice convenience. However, the same method works
15+
with `.pyplot.subplots` or keys that are different than what you want to label
16+
the subplot with.
1417
"""
1518

1619
importmatplotlib.pyplotasplt
1720

18-
importmatplotlib.transformsasmtransforms
19-
21+
# %%
2022
fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
2123
layout='constrained')
22-
2324
forlabel,axinaxs.items():
24-
# label physical distance in and down:
25-
trans=mtransforms.ScaledTranslation(10/72,-5/72,fig.dpi_scale_trans)
26-
ax.text(0.0,1.0,label,transform=ax.transAxes+trans,
27-
fontsize='medium',verticalalignment='top',fontfamily='serif',
28-
bbox=dict(facecolor='0.7',edgecolor='none',pad=3.0))
29-
30-
plt.show()
25+
# Put the label
26+
# - at the top left corner (axes fraction (0, 1)),
27+
# - offset half-a-fontsize right and half-a-fontsize down
28+
# (offset fontsize (+0.5, -0.5)),
29+
# i.e. just inside the axes.
30+
ax.annotate(
31+
label,
32+
xy=(0,1),xycoords='axes fraction',
33+
xytext=(+0.5,-0.5),textcoords='offset fontsize',
34+
fontsize='medium',verticalalignment='top',fontfamily='serif',
35+
bbox=dict(facecolor='0.7',edgecolor='none',pad=3.0))
3136

3237
# %%
33-
# We may prefer the labels outside the axes, but still aligned
34-
# with each other, in which case we use a slightly different transform:
35-
3638
fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
3739
layout='constrained')
38-
3940
forlabel,axinaxs.items():
40-
# label physical distance to the left and up:
41-
trans=mtransforms.ScaledTranslation(-20/72,7/72,fig.dpi_scale_trans)
42-
ax.text(0.0,1.0,label,transform=ax.transAxes+trans,
43-
fontsize='medium',va='bottom',fontfamily='serif')
44-
45-
plt.show()
41+
# Put the label
42+
# - at the top left corner (axes fraction (0, 1)),
43+
# - offset 20 pixels left and 7 pixels up (offset points (-20, +7)),
44+
# i.e. just outside the axes.
45+
ax.annotate(
46+
label,
47+
xy=(0,1),xycoords='axes fraction',
48+
xytext=(-20,+7),textcoords='offset points',
49+
fontsize='medium',va='bottom',fontfamily='serif')
4650

4751
# %%
4852
# If we want it aligned with the title, either incorporate in the title or
4953
# use the *loc* keyword argument:
5054

5155
fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
5256
layout='constrained')
53-
5457
forlabel,axinaxs.items():
5558
ax.set_title('Normal Title',fontstyle='italic')
5659
ax.set_title(label,fontfamily='serif',loc='left',fontsize='medium')
@@ -67,5 +70,4 @@
6770
# - `matplotlib.figure.Figure.subplot_mosaic` /
6871
# `matplotlib.pyplot.subplot_mosaic`
6972
# - `matplotlib.axes.Axes.set_title`
70-
# - `matplotlib.axes.Axes.text`
71-
# - `matplotlib.transforms.ScaledTranslation`
73+
# - `matplotlib.axes.Axes.annotate`

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp