Skip to content

Commit f87338c

Browse files
committed
replace subplot(ijk) calls by subplots(i, j)
1 parent 34f99a9 commit f87338c

File tree

8 files changed

+26
-61
lines changed

8 files changed

+26
-61
lines changed

examples/misc/patheffect_demo.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
from matplotlib import patheffects
99
import numpy as np
1010

11-
plt.figure(figsize=(8, 3))
12-
ax1 = plt.subplot(131)
11+
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 3))
1312
ax1.imshow([[1, 2], [2, 3]])
1413
txt = ax1.annotate("test", (1., 1.), (0., 0),
1514
arrowprops=dict(arrowstyle="->",
@@ -25,7 +24,6 @@
2524
foreground="w")]
2625
ax1.grid(True, linestyle="-", path_effects=pe)
2726

28-
ax2 = plt.subplot(132)
2927
arr = np.arange(25).reshape((5, 5))
3028
ax2.imshow(arr)
3129
cntr = ax2.contour(arr, colors="k")
@@ -38,7 +36,6 @@
3836
patheffects.withStroke(linewidth=3, foreground="w")])
3937

4038
# shadow as a path effect
41-
ax3 = plt.subplot(133)
4239
p1, = ax3.plot([0, 1], [0, 1])
4340
leg = ax3.legend([p1], ["Line 1"], fancybox=True, loc='upper left')
4441
leg.legendPatch.set_path_effects([patheffects.withSimplePatchShadow()])

examples/pie_and_polar_charts/bar_of_pie.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import numpy as np
1616

1717
# make figure and assign axis objects
18-
fig = plt.figure(figsize=(9, 5))
19-
ax1 = fig.add_subplot(121)
20-
ax2 = fig.add_subplot(122)
18+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5))
2119
fig.subplots_adjust(wspace=0)
2220

2321
# pie chart parameters

examples/text_labels_and_annotations/demo_text_path.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,24 @@ def draw(self, renderer=None):
4848

4949
usetex = plt.rcParams["text.usetex"]
5050

51-
fig = plt.figure()
51+
fig, (ax1, ax2) = plt.subplots(2)
5252

5353
# EXAMPLE 1
5454

55-
ax = plt.subplot(211)
56-
5755
arr = plt.imread(get_sample_data("grace_hopper.png"))
5856

5957
text_path = TextPath((0, 0), "!?", size=150)
6058
p = PathClippedImagePatch(text_path, arr, ec="k",
6159
transform=IdentityTransform())
6260

63-
# p.set_clip_on(False)
64-
6561
# make offset box
6662
offsetbox = AuxTransformBox(IdentityTransform())
6763
offsetbox.add_artist(p)
6864

6965
# make anchored offset box
7066
ao = AnchoredOffsetbox(loc='upper left', child=offsetbox, frameon=True,
7167
borderpad=0.2)
72-
ax.add_artist(ao)
68+
ax1.add_artist(ao)
7369

7470
# another text
7571
from matplotlib.patches import PathPatch
@@ -95,16 +91,13 @@ def draw(self, renderer=None):
9591
box_alignment=(1., 0.),
9692
frameon=False
9793
)
98-
ax.add_artist(ab)
94+
ax1.add_artist(ab)
9995

100-
ax.imshow([[0, 1, 2], [1, 2, 3]], cmap=plt.cm.gist_gray_r,
101-
interpolation="bilinear",
102-
aspect="auto")
96+
ax1.imshow([[0, 1, 2], [1, 2, 3]], cmap=plt.cm.gist_gray_r,
97+
interpolation="bilinear", aspect="auto")
10398

10499
# EXAMPLE 2
105100

106-
ax = plt.subplot(212)
107-
108101
arr = np.arange(256).reshape(1, 256) / 256
109102

110103
if usetex:
@@ -131,11 +124,10 @@ def draw(self, renderer=None):
131124
boxcoords="offset points",
132125
box_alignment=(0.5, 0.5),
133126
)
134-
# text_path.set_size(10)
135127

136-
ax.add_artist(ab)
128+
ax2.add_artist(ab)
137129

138-
ax.set_xlim(0, 1)
139-
ax.set_ylim(0, 1)
130+
ax2.set_xlim(0, 1)
131+
ax2.set_ylim(0, 1)
140132

141133
plt.show()

lib/matplotlib/tests/test_axes.py

+11-22
Original file line numberDiff line numberDiff line change
@@ -602,26 +602,21 @@ def test_fill_units():
602602
dt = np.arange('2009-04-27', '2009-04-29', dtype='datetime64[D]')
603603
dtn = mdates.date2num(dt)
604604

605-
fig = plt.figure()
605+
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
606606

607-
# Top-Left
608-
ax1 = fig.add_subplot(221)
609607
ax1.plot([t], [value], yunits='deg', color='red')
610608
ind = [0, 0, 1, 1]
611609
ax1.fill(dtn[ind], [0.0, 0.0, 90.0, 0.0], 'b')
612-
# Top-Right
613-
ax2 = fig.add_subplot(222)
610+
614611
ax2.plot([t], [value], yunits='deg', color='red')
615612
ax2.fill([t, t, t + day, t + day],
616613
[0.0, 0.0, 90.0, 0.0], 'b')
617-
# Bottom-Left
618-
ax3 = fig.add_subplot(223)
614+
619615
ax3.plot([t], [value], yunits='deg', color='red')
620616
ax3.fill(dtn[ind],
621617
[0 * units.deg, 0 * units.deg, 90 * units.deg, 0 * units.deg],
622618
'b')
623-
# Bottom-Right
624-
ax4 = fig.add_subplot(224)
619+
625620
ax4.plot([t], [value], yunits='deg', color='red')
626621
ax4.fill([t, t, t + day, t + day],
627622
[0 * units.deg, 0 * units.deg, 90 * units.deg, 0 * units.deg],
@@ -635,22 +630,16 @@ def test_single_point():
635630
matplotlib.rcParams['lines.marker'] = 'o'
636631
matplotlib.rcParams['axes.grid'] = True
637632

638-
plt.figure()
639-
plt.subplot(211)
640-
plt.plot([0], [0], 'o')
641-
642-
plt.subplot(212)
643-
plt.plot([1], [1], 'o')
633+
fig, (ax1, ax2) = plt.subplots(2)
634+
ax1.plot([0], [0], 'o')
635+
ax2.plot([1], [1], 'o')
644636

645637
# Reuse testcase from above for a labeled data test
646638
data = {'a': [0], 'b': [1]}
647639

648-
plt.figure()
649-
plt.subplot(211)
650-
plt.plot('a', 'a', 'o', data=data)
651-
652-
plt.subplot(212)
653-
plt.plot('b', 'b', 'o', data=data)
640+
fig, (ax1, ax2) = plt.subplots(2)
641+
ax1.plot('a', 'a', 'o', data=data)
642+
ax2.plot('b', 'b', 'o', data=data)
654643

655644

656645
@image_comparison(['single_date.png'], style='mpl20')
@@ -721,7 +710,7 @@ def test_axvspan_epoch():
721710
dt = units.Duration("ET", units.day.convert("sec"))
722711

723712
ax = plt.gca()
724-
plt.axvspan(t0, tf, facecolor="blue", alpha=0.25)
713+
ax.axvspan(t0, tf, facecolor="blue", alpha=0.25)
725714
ax.set_xlim(t0 - 5.0*dt, tf + 5.0*dt)
726715

727716

lib/matplotlib/tests/test_backend_pgf.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,8 @@ def test_bbox_inches():
198198
'pgf.rcfonts': False}
199199
mpl.rcParams.update(rc_xelatex)
200200

201-
Y, X = np.ogrid[-1:1:40j, -1:1:40j]
202-
fig = plt.figure()
203-
ax1 = fig.add_subplot(121)
201+
fig, (ax1, ax2) = plt.subplots(1, 2)
204202
ax1.plot(range(5))
205-
ax2 = fig.add_subplot(122)
206203
ax2.plot(range(5))
207204
plt.tight_layout()
208205

lib/matplotlib/tests/test_contour.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,8 @@ def test_contourf_log_extension():
325325
plt.rcParams['pcolormesh.snap'] = False
326326

327327
# Test that contourf with lognorm is extended correctly
328-
fig = plt.figure(figsize=(10, 5))
328+
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 5))
329329
fig.subplots_adjust(left=0.05, right=0.95)
330-
ax1 = fig.add_subplot(131)
331-
ax2 = fig.add_subplot(132)
332-
ax3 = fig.add_subplot(133)
333330

334331
# make data set with large range e.g. between 1e-8 and 1e10
335332
data_exp = np.linspace(-7.5, 9.5, 1200)

lib/matplotlib/tests/test_image.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@ def test_image_interps():
3030
X = np.arange(100)
3131
X = X.reshape(5, 20)
3232

33-
fig = plt.figure()
34-
ax1 = fig.add_subplot(311)
33+
fig, (ax1, ax2, ax3) = plt.subplots(3)
3534
ax1.imshow(X, interpolation='nearest')
3635
ax1.set_title('three interpolations')
3736
ax1.set_ylabel('nearest')
3837

39-
ax2 = fig.add_subplot(312)
4038
ax2.imshow(X, interpolation='bilinear')
4139
ax2.set_ylabel('bilinear')
4240

43-
ax3 = fig.add_subplot(313)
4441
ax3.imshow(X, interpolation='bicubic')
4542
ax3.set_ylabel('bicubic')
4643

@@ -69,11 +66,9 @@ def test_interp_nearest_vs_none():
6966
rcParams['savefig.dpi'] = 3
7067
X = np.array([[[218, 165, 32], [122, 103, 238]],
7168
[[127, 255, 0], [255, 99, 71]]], dtype=np.uint8)
72-
fig = plt.figure()
73-
ax1 = fig.add_subplot(121)
69+
fig, (ax1, ax2) = plt.subplots(1, 2)
7470
ax1.imshow(X, interpolation='none')
7571
ax1.set_title('interpolation none')
76-
ax2 = fig.add_subplot(122)
7772
ax2.imshow(X, interpolation='nearest')
7873
ax2.set_title('interpolation nearest')
7974

tutorials/text/annotations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@
425425
426426
This allows annotating a point in another axes::
427427
428-
ax1, ax2 = subplot(121), subplot(122)
428+
fig, (ax1, ax2) = plt.subplots(1, 2)
429429
ax2.annotate("Test", xy=(0.5, 0.5), xycoords=ax1.transData,
430430
xytext=(0.5, 0.5), textcoords=ax2.transData,
431431
arrowprops=dict(arrowstyle="->"))

0 commit comments

Comments
 (0)