Skip to content

Commit 8ddc371

Browse files
committed
FIX: annotate_axes encapsulation
1 parent 63054c2 commit 8ddc371

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

tutorials/intermediate/arranging_axes.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
---------------------------------------
1818
1919
`~matplotlib.pyplot.subplots`
20-
The primary function used to create figures and a grid of axes. It is
21-
similar to `.pyplot.subplot`, but creates and places all axes on the
22-
figure at once, and returns an object array with handles for
23-
the axes in the grid. See also `.Figure.subplots`.
20+
The primary function used to create figures and a grid of axes. It
21+
creates and places all axes on the figure at once, and returns an
22+
object array with handles for the axes in the grid. See
23+
`.Figure.subplots`.
2424
2525
or
2626
@@ -70,11 +70,6 @@
7070
.. redirect-from:: /tutorials/intermediate/gridspec
7171
7272
"""
73-
74-
import matplotlib.pyplot as plt
75-
76-
import numpy as np
77-
7873
############################################################################
7974
# High-level methods for making grids
8075
# ===================================
@@ -90,14 +85,30 @@
9085
# use `~.Axes.annotate`, but other examples could be `~.Axes.plot`,
9186
# `~.Axes.pcolormesh`, etc.
9287

88+
import matplotlib.pyplot as plt
89+
import numpy as np
90+
9391
fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(4.5, 3.5),
9492
constrained_layout=True)
93+
# add an artist, in this case a nice label in the middle...
9594
for row in range(2):
9695
for col in range(2):
97-
axs[row, col].annotate(f'axs[{row}, {col}]', (0.1, 0.5),
98-
xycoords='axes fraction', va='center')
96+
axs[row, col].annotate(f'axs[{row}, {col}]', (0.5, 0.5),
97+
transform=axs[row,col].transAxes,
98+
ha='center', va='center', fontsize=18,
99+
color='darkgrey')
99100
fig.suptitle('plt.subplots()')
100101

102+
##############################################################################
103+
# We will annotate a lot of axes, so lets encapsulate the annotation, rather
104+
# than having that large piece of annotation code every time we need it:
105+
106+
107+
def annotate_axes(ax, text, fontsize=18):
108+
ax.text(0.5, 0.5, text, transform=ax.transAxes,
109+
ha="center", va="center", fontsize=fontsize, color="darkgrey")
110+
111+
101112
##############################################################################
102113
# The same effect can be achieved with `~.pyplot.subplot_mosaic`,
103114
# but the return type is a dictionary instead of an array, where the user
@@ -109,8 +120,7 @@
109120
['loleft', 'loright']],
110121
figsize=(4.5, 3.5), constrained_layout=True)
111122
for k in axd.keys():
112-
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
113-
xycoords='axes fraction', va='center')
123+
annotate_axes(axd[k], f'axd["{k}"]', fontsize=14)
114124
fig.suptitle('plt.subplot_mosaic()')
115125

116126
############################################################################
@@ -126,8 +136,7 @@
126136
['loleft', 'right']],
127137
figsize=(4.5, 3.5), constrained_layout=True)
128138
for k in axd.keys():
129-
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
130-
xycoords='axes fraction', va='center')
139+
annotate_axes(axd[k], f'axd["{k}"]', fontsize=14)
131140
fig.suptitle('plt.subplot_mosaic()')
132141

133142
############################################################################
@@ -144,14 +153,13 @@
144153
# can be passed to `~matplotlib.pyplot.subplots` and
145154
# `~matplotlib.pyplot.subplot_mosaic`:
146155

147-
gs_kw = dict(width_ratios=[1, 2.2], height_ratios=[1, 2])
156+
gs_kw = dict(width_ratios=[1.4, 1], height_ratios=[1, 2])
148157
fig, axd = plt.subplot_mosaic([['upleft', 'right'],
149158
['loleft', 'right']],
150159
gridspec_kw=gs_kw, figsize=(4.5, 3.5),
151160
constrained_layout=True)
152161
for k in axd.keys():
153-
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
154-
xycoords='axes fraction', va='center')
162+
annotate_axes(axd[k], f'axd["{k}"]', fontsize=14)
155163
fig.suptitle('plt.subplot_mosaic()')
156164

157165
############################################################################
@@ -190,9 +198,7 @@
190198

191199
fig, axd = plt.subplot_mosaic(outer, constrained_layout=True)
192200
for k in axd.keys():
193-
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
194-
xycoords='axes fraction', va='center')
195-
plt.show()
201+
annotate_axes(axd[k], f'axd["{k}"]')
196202

197203
############################################################################
198204
# Low-level and advanced grid methods
@@ -212,13 +218,13 @@
212218
fig = plt.figure(figsize=(4.5, 3.5), constrained_layout=True)
213219
spec = fig.add_gridspec(ncols=2, nrows=2)
214220
ax0 = fig.add_subplot(spec[0, 0])
215-
ax0.annotate('ax0', (0.1, 0.5), xycoords='axes fraction', va='center')
221+
annotate_axes(ax0, 'ax0')
216222
ax1 = fig.add_subplot(spec[0, 1])
217-
ax1.annotate('ax1', (0.1, 0.5), xycoords='axes fraction', va='center')
223+
annotate_axes(ax1, 'ax0')
218224
ax2 = fig.add_subplot(spec[1, 0])
219-
ax2.annotate('ax2', (0.1, 0.5), xycoords='axes fraction', va='center')
225+
annotate_axes(ax2, 'ax0')
220226
ax3 = fig.add_subplot(spec[1, 1])
221-
ax3.annotate('ax3', (0.1, 0.5), xycoords='axes fraction', va='center')
227+
annotate_axes(ax3, 'ax0')
222228
fig.suptitle('Manually added subplots using add_gridspec')
223229

224230
#############################################################################
@@ -232,11 +238,11 @@
232238
fig = plt.figure(figsize=(4.5, 3.5), constrained_layout=True)
233239
spec = fig.add_gridspec(2, 2)
234240
ax0 = fig.add_subplot(spec[0, :])
235-
ax0.annotate('ax0', (0.1, 0.5), xycoords='axes fraction', va='center')
241+
annotate_axes(ax0, 'ax0')
236242
ax10 = fig.add_subplot(spec[1, 0])
237-
ax10.annotate('ax10', (0.1, 0.5), xycoords='axes fraction', va='center')
243+
annotate_axes(ax10, 'ax10')
238244
ax11 = fig.add_subplot(spec[1, 1])
239-
ax11.annotate('ax11', (0.1, 0.5), xycoords='axes fraction', va='center')
245+
annotate_axes(ax11, 'ax11')
240246
fig.suptitle('Manually added subplots, spanning a column')
241247

242248
###############################################################################
@@ -254,13 +260,13 @@
254260
gs = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.75,
255261
hspace=0.1, wspace=0.05)
256262
ax0 = fig.add_subplot(gs[:-1, :])
257-
ax0.annotate('ax0', (0.1, 0.5), xycoords='axes fraction', va='center')
263+
annotate_axes(ax0, 'ax0')
258264
ax1 = fig.add_subplot(gs[-1, :-1])
259-
ax1.annotate('ax1', (0.1, 0.5), xycoords='axes fraction', va='center')
265+
annotate_axes(ax1, 'ax1')
260266
ax2 = fig.add_subplot(gs[-1, -1])
261-
ax2.annotate('ax2', (0.1, 0.5), xycoords='axes fraction', va='center')
267+
annotate_axes(ax2, 'ax2')
262268
fig.suptitle('Manual gridspec with right=0.75')
263-
plt.show()
269+
264270
###############################################################################
265271
# Nested layouts with SubplotSpec
266272
# ===============================
@@ -281,14 +287,12 @@
281287
for a in range(2):
282288
for b in range(2):
283289
ax = fig.add_subplot(gs00[a, b])
284-
ax.annotate(f'axLeft[{a}, {b}]', (0.1, 0.5), xycoords='axes fraction',
285-
va='center')
290+
annotate_axes(ax, f'axLeft[{a}, {b}]', fontsize=10)
286291
if a == 1 and b == 1:
287292
ax.set_xlabel('xlabel')
288293
for a in range(3):
289294
ax = fig.add_subplot(gs01[a])
290-
ax.annotate(f'axRight[{a}]', (0.1, 0.5), xycoords='axes fraction',
291-
va='center')
295+
annotate_axes(ax, f'axRight[{a}, {b}]')
292296
if a == 2:
293297
ax.set_ylabel('ylabel')
294298

0 commit comments

Comments
 (0)