diff --git a/examples/misc/patheffect_demo.py b/examples/misc/patheffect_demo.py index 618877b1a48e..b5f59ce106fd 100644 --- a/examples/misc/patheffect_demo.py +++ b/examples/misc/patheffect_demo.py @@ -8,8 +8,7 @@ from matplotlib import patheffects import numpy as np -plt.figure(figsize=(8, 3)) -ax1 = plt.subplot(131) +fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 3)) ax1.imshow([[1, 2], [2, 3]]) txt = ax1.annotate("test", (1., 1.), (0., 0), arrowprops=dict(arrowstyle="->", @@ -25,7 +24,6 @@ foreground="w")] ax1.grid(True, linestyle="-", path_effects=pe) -ax2 = plt.subplot(132) arr = np.arange(25).reshape((5, 5)) ax2.imshow(arr) cntr = ax2.contour(arr, colors="k") @@ -38,7 +36,6 @@ patheffects.withStroke(linewidth=3, foreground="w")]) # shadow as a path effect -ax3 = plt.subplot(133) p1, = ax3.plot([0, 1], [0, 1]) leg = ax3.legend([p1], ["Line 1"], fancybox=True, loc='upper left') leg.legendPatch.set_path_effects([patheffects.withSimplePatchShadow()]) diff --git a/examples/pie_and_polar_charts/bar_of_pie.py b/examples/pie_and_polar_charts/bar_of_pie.py index 1efcfc976924..92f54ea63bf0 100644 --- a/examples/pie_and_polar_charts/bar_of_pie.py +++ b/examples/pie_and_polar_charts/bar_of_pie.py @@ -15,9 +15,7 @@ import numpy as np # make figure and assign axis objects -fig = plt.figure(figsize=(9, 5)) -ax1 = fig.add_subplot(121) -ax2 = fig.add_subplot(122) +fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5)) fig.subplots_adjust(wspace=0) # pie chart parameters diff --git a/examples/text_labels_and_annotations/demo_text_path.py b/examples/text_labels_and_annotations/demo_text_path.py index 461268427cfc..fb208718dd86 100644 --- a/examples/text_labels_and_annotations/demo_text_path.py +++ b/examples/text_labels_and_annotations/demo_text_path.py @@ -48,20 +48,16 @@ def draw(self, renderer=None): usetex = plt.rcParams["text.usetex"] - fig = plt.figure() + fig, (ax1, ax2) = plt.subplots(2) # EXAMPLE 1 - ax = plt.subplot(211) - arr = plt.imread(get_sample_data("grace_hopper.png")) text_path = TextPath((0, 0), "!?", size=150) p = PathClippedImagePatch(text_path, arr, ec="k", transform=IdentityTransform()) - # p.set_clip_on(False) - # make offset box offsetbox = AuxTransformBox(IdentityTransform()) offsetbox.add_artist(p) @@ -69,7 +65,7 @@ def draw(self, renderer=None): # make anchored offset box ao = AnchoredOffsetbox(loc='upper left', child=offsetbox, frameon=True, borderpad=0.2) - ax.add_artist(ao) + ax1.add_artist(ao) # another text from matplotlib.patches import PathPatch @@ -95,16 +91,13 @@ def draw(self, renderer=None): box_alignment=(1., 0.), frameon=False ) - ax.add_artist(ab) + ax1.add_artist(ab) - ax.imshow([[0, 1, 2], [1, 2, 3]], cmap=plt.cm.gist_gray_r, - interpolation="bilinear", - aspect="auto") + ax1.imshow([[0, 1, 2], [1, 2, 3]], cmap=plt.cm.gist_gray_r, + interpolation="bilinear", aspect="auto") # EXAMPLE 2 - ax = plt.subplot(212) - arr = np.arange(256).reshape(1, 256) / 256 if usetex: @@ -131,11 +124,10 @@ def draw(self, renderer=None): boxcoords="offset points", box_alignment=(0.5, 0.5), ) - # text_path.set_size(10) - ax.add_artist(ab) + ax2.add_artist(ab) - ax.set_xlim(0, 1) - ax.set_ylim(0, 1) + ax2.set_xlim(0, 1) + ax2.set_ylim(0, 1) plt.show() diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 063fe8f186c6..f0380ac7481c 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -602,26 +602,21 @@ def test_fill_units(): dt = np.arange('2009-04-27', '2009-04-29', dtype='datetime64[D]') dtn = mdates.date2num(dt) - fig = plt.figure() + fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) - # Top-Left - ax1 = fig.add_subplot(221) ax1.plot([t], [value], yunits='deg', color='red') ind = [0, 0, 1, 1] ax1.fill(dtn[ind], [0.0, 0.0, 90.0, 0.0], 'b') - # Top-Right - ax2 = fig.add_subplot(222) + ax2.plot([t], [value], yunits='deg', color='red') ax2.fill([t, t, t + day, t + day], [0.0, 0.0, 90.0, 0.0], 'b') - # Bottom-Left - ax3 = fig.add_subplot(223) + ax3.plot([t], [value], yunits='deg', color='red') ax3.fill(dtn[ind], [0 * units.deg, 0 * units.deg, 90 * units.deg, 0 * units.deg], 'b') - # Bottom-Right - ax4 = fig.add_subplot(224) + ax4.plot([t], [value], yunits='deg', color='red') ax4.fill([t, t, t + day, t + day], [0 * units.deg, 0 * units.deg, 90 * units.deg, 0 * units.deg], @@ -635,22 +630,16 @@ def test_single_point(): matplotlib.rcParams['lines.marker'] = 'o' matplotlib.rcParams['axes.grid'] = True - plt.figure() - plt.subplot(211) - plt.plot([0], [0], 'o') - - plt.subplot(212) - plt.plot([1], [1], 'o') + fig, (ax1, ax2) = plt.subplots(2) + ax1.plot([0], [0], 'o') + ax2.plot([1], [1], 'o') # Reuse testcase from above for a labeled data test data = {'a': [0], 'b': [1]} - plt.figure() - plt.subplot(211) - plt.plot('a', 'a', 'o', data=data) - - plt.subplot(212) - plt.plot('b', 'b', 'o', data=data) + fig, (ax1, ax2) = plt.subplots(2) + ax1.plot('a', 'a', 'o', data=data) + ax2.plot('b', 'b', 'o', data=data) @image_comparison(['single_date.png'], style='mpl20') @@ -721,7 +710,7 @@ def test_axvspan_epoch(): dt = units.Duration("ET", units.day.convert("sec")) ax = plt.gca() - plt.axvspan(t0, tf, facecolor="blue", alpha=0.25) + ax.axvspan(t0, tf, facecolor="blue", alpha=0.25) ax.set_xlim(t0 - 5.0*dt, tf + 5.0*dt) diff --git a/lib/matplotlib/tests/test_backend_pgf.py b/lib/matplotlib/tests/test_backend_pgf.py index 702cc6c35870..829e46ae5ceb 100644 --- a/lib/matplotlib/tests/test_backend_pgf.py +++ b/lib/matplotlib/tests/test_backend_pgf.py @@ -198,11 +198,8 @@ def test_bbox_inches(): 'pgf.rcfonts': False} mpl.rcParams.update(rc_xelatex) - Y, X = np.ogrid[-1:1:40j, -1:1:40j] - fig = plt.figure() - ax1 = fig.add_subplot(121) + fig, (ax1, ax2) = plt.subplots(1, 2) ax1.plot(range(5)) - ax2 = fig.add_subplot(122) ax2.plot(range(5)) plt.tight_layout() diff --git a/lib/matplotlib/tests/test_contour.py b/lib/matplotlib/tests/test_contour.py index 3de968bbba67..c5844d2da563 100644 --- a/lib/matplotlib/tests/test_contour.py +++ b/lib/matplotlib/tests/test_contour.py @@ -325,11 +325,8 @@ def test_contourf_log_extension(): plt.rcParams['pcolormesh.snap'] = False # Test that contourf with lognorm is extended correctly - fig = plt.figure(figsize=(10, 5)) + fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 5)) fig.subplots_adjust(left=0.05, right=0.95) - ax1 = fig.add_subplot(131) - ax2 = fig.add_subplot(132) - ax3 = fig.add_subplot(133) # make data set with large range e.g. between 1e-8 and 1e10 data_exp = np.linspace(-7.5, 9.5, 1200) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 8c2214038ac3..7e7f85085fff 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -27,20 +27,16 @@ def test_image_interps(): # Remove this line when this test image is regenerated. plt.rcParams['text.kerning_factor'] = 6 - X = np.arange(100) - X = X.reshape(5, 20) + X = np.arange(100).reshape(5, 20) - fig = plt.figure() - ax1 = fig.add_subplot(311) + fig, (ax1, ax2, ax3) = plt.subplots(3) ax1.imshow(X, interpolation='nearest') ax1.set_title('three interpolations') ax1.set_ylabel('nearest') - ax2 = fig.add_subplot(312) ax2.imshow(X, interpolation='bilinear') ax2.set_ylabel('bilinear') - ax3 = fig.add_subplot(313) ax3.imshow(X, interpolation='bicubic') ax3.set_ylabel('bicubic') @@ -69,11 +65,9 @@ def test_interp_nearest_vs_none(): rcParams['savefig.dpi'] = 3 X = np.array([[[218, 165, 32], [122, 103, 238]], [[127, 255, 0], [255, 99, 71]]], dtype=np.uint8) - fig = plt.figure() - ax1 = fig.add_subplot(121) + fig, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(X, interpolation='none') ax1.set_title('interpolation none') - ax2 = fig.add_subplot(122) ax2.imshow(X, interpolation='nearest') ax2.set_title('interpolation nearest') diff --git a/tutorials/text/annotations.py b/tutorials/text/annotations.py index aeada6ea1c9c..878835b2f7b7 100644 --- a/tutorials/text/annotations.py +++ b/tutorials/text/annotations.py @@ -425,7 +425,7 @@ This allows annotating a point in another axes:: - ax1, ax2 = subplot(121), subplot(122) + fig, (ax1, ax2) = plt.subplots(1, 2) ax2.annotate("Test", xy=(0.5, 0.5), xycoords=ax1.transData, xytext=(0.5, 0.5), textcoords=ax2.transData, arrowprops=dict(arrowstyle="->"))