diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index f9f447f37813..f14a781c86e7 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2425,8 +2425,7 @@ def test_pyplot_axes(): @image_comparison(['log_scales']) def test_log_scales(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.plot(np.log(np.linspace(0.1, 100))) ax.set_yscale('log', base=5.5) ax.invert_yaxis() @@ -2441,8 +2440,7 @@ def test_log_scales_no_data(): def test_log_scales_invalid(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.set_xscale('log') with pytest.warns(UserWarning, match='Attempted to set non-positive'): ax.set_xlim(-1, 10) @@ -2465,8 +2463,7 @@ def test_stackplot(): # Reuse testcase from above for a labeled data test data = {"x": x, "y1": y1, "y2": y2, "y3": y3} - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.stackplot("x", "y1", "y2", "y3", data=data) ax.set_xlim((0, 10)) ax.set_ylim((0, 70)) @@ -3317,27 +3314,26 @@ def test_errorbar_limits(): yerr = 0.2 ls = 'dotted' - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() # standard error bars - plt.errorbar(x, y, xerr=xerr, yerr=yerr, ls=ls, color='blue') + ax.errorbar(x, y, xerr=xerr, yerr=yerr, ls=ls, color='blue') # including upper limits uplims = np.zeros_like(x) uplims[[1, 5, 9]] = True - plt.errorbar(x, y+0.5, xerr=xerr, yerr=yerr, uplims=uplims, ls=ls, - color='green') + ax.errorbar(x, y+0.5, xerr=xerr, yerr=yerr, uplims=uplims, ls=ls, + color='green') # including lower limits lolims = np.zeros_like(x) lolims[[2, 4, 8]] = True - plt.errorbar(x, y+1.0, xerr=xerr, yerr=yerr, lolims=lolims, ls=ls, - color='red') + ax.errorbar(x, y+1.0, xerr=xerr, yerr=yerr, lolims=lolims, ls=ls, + color='red') # including upper and lower limits - plt.errorbar(x, y+1.5, marker='o', ms=8, xerr=xerr, yerr=yerr, - lolims=lolims, uplims=uplims, ls=ls, color='magenta') + ax.errorbar(x, y+1.5, marker='o', ms=8, xerr=xerr, yerr=yerr, + lolims=lolims, uplims=uplims, ls=ls, color='magenta') # including xlower and xupper limits xerr = 0.2 @@ -3349,10 +3345,10 @@ def test_errorbar_limits(): uplims = np.zeros_like(x) lolims[[6]] = True uplims[[3]] = True - plt.errorbar(x, y+2.1, marker='o', ms=8, xerr=xerr, yerr=yerr, - xlolims=xlolims, xuplims=xuplims, uplims=uplims, - lolims=lolims, ls='none', mec='blue', capsize=0, - color='cyan') + ax.errorbar(x, y+2.1, marker='o', ms=8, xerr=xerr, yerr=yerr, + xlolims=xlolims, xuplims=xuplims, uplims=uplims, + lolims=lolims, ls='none', mec='blue', capsize=0, + color='cyan') ax.set_xlim((0, 5.5)) ax.set_title('Errorbar upper and lower limits') @@ -3484,13 +3480,11 @@ def test_stem(use_line_collection): ax.stem(x, np.cos(x), linefmt='C2-.', markerfmt='k+', basefmt='C1-.', label=' ', use_line_collection=use_line_collection) - ax.legend() def test_stem_args(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() x = list(range(10)) y = list(range(10)) @@ -4229,8 +4223,7 @@ def test_step_linestyle(): @image_comparison(['mixed_collection'], remove_text=True) def test_mixed_collection(): # First illustrate basic pyplot interface, using defaults where possible. - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() c = mpatches.Circle((8, 8), radius=4, facecolor='none', edgecolor='green') @@ -4571,8 +4564,7 @@ def test_rcparam_grid_minor(): for locator, result in values: matplotlib.rcParams['axes.grid.which'] = locator - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() assert (ax.xaxis._gridOnMajor, ax.xaxis._gridOnMinor) == result matplotlib.rcParams['axes.grid'] = orig_grid @@ -5786,16 +5778,14 @@ def test_title_no_move_off_page(): def test_offset_label_color(): # Tests issue 6440 - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.plot([1.01e9, 1.02e9, 1.03e9]) ax.yaxis.set_tick_params(labelcolor='red') assert ax.yaxis.get_offset_text().get_color() == 'red' def test_offset_text_visible(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.plot([1.01e9, 1.02e9, 1.03e9]) ax.yaxis.set_tick_params(label1On=False, label2On=True) assert ax.yaxis.get_offset_text().get_visible() diff --git a/lib/matplotlib/tests/test_backend_pdf.py b/lib/matplotlib/tests/test_backend_pdf.py index ca5a89ad251f..bdbe41a834c9 100644 --- a/lib/matplotlib/tests/test_backend_pdf.py +++ b/lib/matplotlib/tests/test_backend_pdf.py @@ -31,8 +31,7 @@ def test_use14corefonts(): and containing some French characters and the euro symbol: "Merci pépé pour les 10 €"''' - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.set_title('Test PDF backend with option use14corefonts=True') ax.text(0.5, 0.5, text, horizontalalignment='center', verticalalignment='bottom', @@ -111,8 +110,7 @@ def test_composite_image(): # (on a single set of axes) into a single composite image. X, Y = np.meshgrid(np.arange(-5, 5, 1), np.arange(-5, 5, 1)) Z = np.sin(Y ** 2) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.set_xlim(0, 3) ax.imshow(Z, extent=[0, 1, 0, 1]) ax.imshow(Z[::-1], extent=[2, 3, 0, 1]) diff --git a/lib/matplotlib/tests/test_backend_svg.py b/lib/matplotlib/tests/test_backend_svg.py index 7b3031749c35..66ec48a477bf 100644 --- a/lib/matplotlib/tests/test_backend_svg.py +++ b/lib/matplotlib/tests/test_backend_svg.py @@ -40,8 +40,7 @@ def test_visibility(): @image_comparison(['fill_black_with_alpha.svg'], remove_text=True) def test_fill_black_with_alpha(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.scatter(x=[0, 0.1, 1], y=[0, 0, 0], c='k', alpha=0.1, s=10000) @@ -50,8 +49,7 @@ def test_noscale(): X, Y = np.meshgrid(np.arange(-5, 5, 1), np.arange(-5, 5, 1)) Z = np.sin(Y ** 2) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.imshow(Z, cmap='gray', interpolation='none') @@ -71,8 +69,7 @@ def test_text_urls(): @image_comparison(['bold_font_output.svg']) def test_bold_font_output(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.plot(np.arange(10), np.arange(10)) ax.set_xlabel('nonbold-xlabel') ax.set_ylabel('bold-ylabel', fontweight='bold') @@ -82,8 +79,7 @@ def test_bold_font_output(): @image_comparison(['bold_font_output_with_none_fonttype.svg']) def test_bold_font_output_with_none_fonttype(): plt.rcParams['svg.fonttype'] = 'none' - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.plot(np.arange(10), np.arange(10)) ax.set_xlabel('nonbold-xlabel') ax.set_ylabel('bold-ylabel', fontweight='bold') diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index e40b68cfffa3..3159e7f2bba7 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -36,8 +36,7 @@ def generate_EventCollection_plot(): antialiased=antialiased ) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.add_collection(coll) ax.set_title('EventCollection: default') props = {'positions': positions, @@ -519,8 +518,7 @@ def test_joinstyle(): @image_comparison(['cap_and_joinstyle.png']) def test_cap_and_joinstyle_image(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.set_xlim([-0.5, 1.5]) ax.set_ylim([-0.5, 2.5]) diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 577af4c972b8..dc451ce17e46 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -74,18 +74,16 @@ def test_date_empty(): # make sure we do the right thing when told to plot dates even # if no date data has been presented, cf # http://sourceforge.net/tracker/?func=detail&aid=2850075&group_id=80706&atid=560720 - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.xaxis_date() @image_comparison(['date_axhspan.png']) def test_date_axhspan(): - # test ax hspan with date inputs + # test axhspan with date inputs t0 = datetime.datetime(2009, 1, 20) tf = datetime.datetime(2009, 1, 21) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.axhspan(t0, tf, facecolor="blue", alpha=0.25) ax.set_ylim(t0 - datetime.timedelta(days=5), tf + datetime.timedelta(days=5)) @@ -94,11 +92,10 @@ def test_date_axhspan(): @image_comparison(['date_axvspan.png']) def test_date_axvspan(): - # test ax hspan with date inputs + # test axvspan with date inputs t0 = datetime.datetime(2000, 1, 20) tf = datetime.datetime(2010, 1, 21) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.axvspan(t0, tf, facecolor="blue", alpha=0.25) ax.set_xlim(t0 - datetime.timedelta(days=720), tf + datetime.timedelta(days=720)) @@ -107,11 +104,10 @@ def test_date_axvspan(): @image_comparison(['date_axhline.png']) def test_date_axhline(): - # test ax hline with date inputs + # test axhline with date inputs t0 = datetime.datetime(2009, 1, 20) tf = datetime.datetime(2009, 1, 31) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.axhline(t0, color="blue", lw=3) ax.set_ylim(t0 - datetime.timedelta(days=5), tf + datetime.timedelta(days=5)) @@ -120,11 +116,10 @@ def test_date_axhline(): @image_comparison(['date_axvline.png']) def test_date_axvline(): - # test ax hline with date inputs + # test axvline with date inputs t0 = datetime.datetime(2000, 1, 20) tf = datetime.datetime(2000, 1, 21) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.axvline(t0, color="red", lw=3) ax.set_xlim(t0 - datetime.timedelta(days=5), tf + datetime.timedelta(days=5)) @@ -140,8 +135,7 @@ def test_too_many_date_ticks(caplog): caplog.set_level("WARNING") t0 = datetime.datetime(2000, 1, 20) tf = datetime.datetime(2000, 1, 20) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() with pytest.warns(UserWarning) as rec: ax.set_xlim((t0, tf), auto=True) assert len(rec) == 1 @@ -733,8 +727,7 @@ def test_date_inverted_limit(): # test ax hline with date inputs t0 = datetime.datetime(2009, 1, 20) tf = datetime.datetime(2009, 1, 31) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.axhline(t0, color="blue", lw=3) ax.set_ylim(t0 - datetime.timedelta(days=5), tf + datetime.timedelta(days=5)) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 6ffe0e7a2535..dc1495a4328d 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -215,8 +215,7 @@ def test_hatching(): def test_legend_remove(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() lines = ax.plot(range(10)) leg = fig.legend(lines, "test") leg.remove() diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index 0fe5b58a0de4..b799a4aa0c60 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -73,8 +73,7 @@ def test_invisible_Line_rendering(): def test_set_line_coll_dash(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() np.random.seed(0) # Testing setting linestyles for line collections. # This should not produce an error. @@ -83,15 +82,13 @@ def test_set_line_coll_dash(): @image_comparison(['line_dashes'], remove_text=True) def test_line_dashes(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.plot(range(10), linestyle=(0, (3, 3)), lw=5) def test_line_colors(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.plot(range(10), color='none') ax.plot(range(10), color='r') ax.plot(range(10), color='.3') @@ -107,8 +104,7 @@ def test_valid_colors(): def test_linestyle_variants(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() for ls in ["-", "solid", "--", "dashed", "-.", "dashdot", ":", "dotted"]: ax.plot(range(10), linestyle=ls) @@ -155,8 +151,7 @@ def test_set_drawstyle(): @image_comparison(['line_collection_dashes'], remove_text=True, style='mpl20') def test_set_line_coll_dash_image(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() np.random.seed(0) ax.contour(np.random.randn(20, 30), linestyles=[(0, (3, 3))]) diff --git a/lib/matplotlib/tests/test_patches.py b/lib/matplotlib/tests/test_patches.py index baff5dabccaa..ea289b1e77d2 100644 --- a/lib/matplotlib/tests/test_patches.py +++ b/lib/matplotlib/tests/test_patches.py @@ -226,8 +226,7 @@ def test_patch_linestyle_accents(): linestyles = ["-", "--", "-.", ":", "solid", "dashed", "dashdot", "dotted"] - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() for i, ls in enumerate(linestyles): star = mpath.Path(verts + i, codes) patch = mpatches.PathPatch(star, diff --git a/lib/matplotlib/tests/test_spines.py b/lib/matplotlib/tests/test_spines.py index 4be97e2b58d5..5e9ec881ac9f 100644 --- a/lib/matplotlib/tests/test_spines.py +++ b/lib/matplotlib/tests/test_spines.py @@ -23,8 +23,7 @@ def test_spines_axes_positions(): @image_comparison(['spines_data_positions']) def test_spines_data_positions(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.spines['left'].set_position(('data', -1.5)) ax.spines['top'].set_position(('data', 0.5)) ax.spines['right'].set_position(('data', -0.5)) @@ -58,15 +57,13 @@ def test_spine_nonlinear_data_positions(fig_test, fig_ref): def test_spines_capstyle(): # issue 2542 plt.rc('axes', linewidth=20) - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() ax.set_xticks([]) ax.set_yticks([]) def test_label_without_ticks(): - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) + fig, ax = plt.subplots() plt.subplots_adjust(left=0.3, bottom=0.3) ax.plot(np.arange(10)) ax.yaxis.set_ticks_position('left')