From a7148e3809e171814a1d5caf46a77f5f193659a9 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 29 Jun 2023 09:44:08 -0700 Subject: [PATCH 1/4] centeralize mpl_cleanup, make hist_df smaller --- pandas/tests/plotting/conftest.py | 34 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/pandas/tests/plotting/conftest.py b/pandas/tests/plotting/conftest.py index 6f77356eb3762..441649168bed6 100644 --- a/pandas/tests/plotting/conftest.py +++ b/pandas/tests/plotting/conftest.py @@ -1,3 +1,5 @@ +import gc + import numpy as np import pytest @@ -8,32 +10,28 @@ @pytest.fixture(autouse=True) -def non_interactive(): - mpl = pytest.importorskip("matplotlib") - mpl.use("template") - yield - - -@pytest.fixture(autouse=True) -def reset_rcParams(): +def mpl_cleanup(): + # matplotlib/testing/decorators.py#L24 + # 1) Resets units registry + # 2) Resets rc_context + # 3) Closes all figures mpl = pytest.importorskip("matplotlib") + mpl_units = pytest.importorskip("matplotlib.units") + plt = pytest.importorskip("matplotlib.pyplot") + orig_units_registry = mpl_units.registry.copy() with mpl.rc_context(): + mpl.use("template") yield - - -@pytest.fixture(autouse=True) -def close_all_figures(): - # https://stackoverflow.com/q/31156578 - yield - plt = pytest.importorskip("matplotlib.pyplot") - plt.cla() - plt.clf() + mpl_units.registry.clear() + mpl_units.registry.update(orig_units_registry) plt.close("all") + # https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#garbage-collection-is-no-longer-run-on-figure-close # noqa: E501 + gc.collect(1) @pytest.fixture def hist_df(): - n = 100 + n = 20 np_random = np.random.RandomState(42) gender = np_random.choice(["Male", "Female"], size=n) classroom = np_random.choice(["A", "B", "C"], size=n) From 2d3397359a0eb627841495b29d2e0360c0fc6eb3 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 29 Jun 2023 09:56:20 -0700 Subject: [PATCH 2/4] remove def close --- pandas/_testing/__init__.py | 2 - pandas/_testing/_io.py | 17 --------- pandas/tests/plotting/common.py | 6 ++- pandas/tests/plotting/frame/test_frame.py | 27 ++------------ .../tests/plotting/frame/test_frame_color.py | 37 +------------------ .../plotting/frame/test_frame_subplots.py | 1 - pandas/tests/plotting/test_boxplot_method.py | 5 +-- pandas/tests/plotting/test_datetimelike.py | 3 -- pandas/tests/plotting/test_groupby.py | 8 ---- pandas/tests/plotting/test_hist_method.py | 5 --- pandas/tests/plotting/test_series.py | 14 ------- 11 files changed, 11 insertions(+), 114 deletions(-) diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index fbbdfa4b8a5bf..ddb3acb7397e6 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -50,7 +50,6 @@ bdate_range, ) from pandas._testing._io import ( - close, round_trip_localpath, round_trip_pathlib, round_trip_pickle, @@ -1094,7 +1093,6 @@ def shares_memory(left, right) -> bool: "box_expected", "BYTES_DTYPES", "can_set_locale", - "close", "COMPLEX_DTYPES", "convert_rows_list_to_csv_str", "DATETIME64_DTYPES", diff --git a/pandas/_testing/_io.py b/pandas/_testing/_io.py index fa0bc58a132d4..63e2243670e9f 100644 --- a/pandas/_testing/_io.py +++ b/pandas/_testing/_io.py @@ -167,20 +167,3 @@ def write_to_compressed(compression, path, data, dest: str = "test"): with compress_method(path, mode=mode) as f: getattr(f, method)(*args) - - -# ------------------------------------------------------------------ -# Plotting - - -def close(fignum=None) -> None: - from matplotlib.pyplot import ( - close as _close, - get_fignums, - ) - - if fignum is None: - for fignum in get_fignums(): - _close(fignum) - else: - _close(fignum) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 8b357754085c8..9dce817c23169 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -77,6 +77,8 @@ def _check_data(xp, rs): xp : matplotlib Axes object rs : matplotlib Axes object """ + import matplotlib.pyplot as plt + xp_lines = xp.get_lines() rs_lines = rs.get_lines() @@ -86,7 +88,7 @@ def _check_data(xp, rs): rsdata = rsl.get_xydata() tm.assert_almost_equal(xpdata, rsdata) - tm.close() + plt.close("all") def _check_visible(collections, visible=True): @@ -538,7 +540,7 @@ def _check_plot_works(f, default_axes=False, **kwargs): plt.savefig(path) finally: - tm.close(fig) + plt.close(fig) return ret diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index f79d651384c56..f3b3fb43edf36 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -47,6 +47,7 @@ from pandas.io.formats.printing import pprint_thing mpl = pytest.importorskip("matplotlib") +plt = pytest.importorskip("matplotlib.pyplot") class TestDataFramePlots: @@ -323,8 +324,6 @@ def test_xcompat(self): assert not isinstance(lines[0].get_xdata(), PeriodIndex) _check_ticks_props(ax, xrot=30) - tm.close() - def test_xcompat_plot_params(self): df = tm.makeTimeDataFrame() plotting.plot_params["xaxis.compat"] = True @@ -333,8 +332,6 @@ def test_xcompat_plot_params(self): assert not isinstance(lines[0].get_xdata(), PeriodIndex) _check_ticks_props(ax, xrot=30) - tm.close() - def test_xcompat_plot_params_x_compat(self): df = tm.makeTimeDataFrame() plotting.plot_params["x_compat"] = False @@ -344,8 +341,6 @@ def test_xcompat_plot_params_x_compat(self): assert not isinstance(lines[0].get_xdata(), PeriodIndex) assert isinstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex) - tm.close() - def test_xcompat_plot_params_context_manager(self): df = tm.makeTimeDataFrame() # useful if you're plotting a bunch together @@ -355,8 +350,6 @@ def test_xcompat_plot_params_context_manager(self): assert not isinstance(lines[0].get_xdata(), PeriodIndex) _check_ticks_props(ax, xrot=30) - tm.close() - def test_xcompat_plot_period(self): df = tm.makeTimeDataFrame() ax = df.plot() @@ -376,7 +369,6 @@ def test_period_compat(self): df.plot() mpl.pyplot.axhline(y=0) - tm.close() @pytest.mark.parametrize("index_dtype", [np.int64, np.float64]) def test_unsorted_index(self, index_dtype): @@ -390,7 +382,6 @@ def test_unsorted_index(self, index_dtype): rs = lines.get_xydata() rs = Series(rs[:, 1], rs[:, 0], dtype=np.int64, name="y") tm.assert_series_equal(rs, df.y, check_index_type=False) - tm.close() @pytest.mark.parametrize( "df", @@ -956,14 +947,12 @@ def test_boxplot(self, hist_df): ax.xaxis.get_ticklocs(), np.arange(1, len(numeric_cols) + 1) ) assert len(ax.lines) == 7 * len(numeric_cols) - tm.close() def test_boxplot_series(self, hist_df): df = hist_df series = df["height"] axes = series.plot.box(rot=40) _check_ticks_props(axes, xrot=40, yrot=0) - tm.close() _check_plot_works(series.plot.box) @@ -1093,7 +1082,6 @@ def test_hist_df_series(self): series = Series(np.random.rand(10)) axes = series.plot.hist(rot=40) _check_ticks_props(axes, xrot=40, yrot=0) - tm.close() def test_hist_df_series_cumulative_density(self): from matplotlib.patches import Rectangle @@ -1103,7 +1091,6 @@ def test_hist_df_series_cumulative_density(self): # height of last bin (index 5) must be 1.0 rects = [x for x in ax.get_children() if isinstance(x, Rectangle)] tm.assert_almost_equal(rects[-1].get_height(), 1.0) - tm.close() def test_hist_df_series_cumulative(self): from matplotlib.patches import Rectangle @@ -1113,7 +1100,6 @@ def test_hist_df_series_cumulative(self): rects = [x for x in ax.get_children() if isinstance(x, Rectangle)] tm.assert_almost_equal(rects[-2].get_height(), 10.0) - tm.close() def test_hist_df_orientation(self): df = DataFrame(np.random.randn(10, 4)) @@ -1801,8 +1787,6 @@ def test_errorbar_asymmetrical(self): with pytest.raises(ValueError, match=msg): df.plot(yerr=err.T) - tm.close() - def test_table(self): df = DataFrame(np.random.rand(10, 3), index=list(string.ascii_letters[:10])) _check_plot_works(df.plot, table=True) @@ -1897,13 +1881,12 @@ def _check(axes): df.plot(x="a", y="b", title="title", ax=ax, sharex=True) gs.tight_layout(plt.gcf()) _check(axes) - tm.close() + plt.close("all") gs, axes = _generate_4_axes_via_gridspec() with tm.assert_produces_warning(UserWarning): axes = df.plot(subplots=True, ax=axes, sharex=True) _check(axes) - tm.close() def test_sharex_false_and_ax(self): # https://github.com/pandas-dev/pandas/issues/9737 using gridspec, @@ -1930,7 +1913,6 @@ def test_sharex_false_and_ax(self): _check_visible(ax.get_yticklabels(), visible=True) _check_visible(ax.get_xticklabels(), visible=True) _check_visible(ax.get_xticklabels(minor=True), visible=True) - tm.close() def test_sharey_and_ax(self): # https://github.com/pandas-dev/pandas/issues/9737 using gridspec, @@ -1963,7 +1945,7 @@ def _check(axes): df.plot(x="a", y="b", title="title", ax=ax, sharey=True) gs.tight_layout(plt.gcf()) _check(axes) - tm.close() + plt.close("all") gs, axes = _generate_4_axes_via_gridspec() with tm.assert_produces_warning(UserWarning): @@ -1971,7 +1953,6 @@ def _check(axes): gs.tight_layout(plt.gcf()) _check(axes) - tm.close() def test_sharey_and_ax_tight(self): # https://github.com/pandas-dev/pandas/issues/9737 using gridspec, @@ -2021,7 +2002,7 @@ def test_memory_leak(self, kind): ref = weakref.ref(df.plot(kind=kind, **args)) # have matplotlib delete all the figures - tm.close() + plt.close("all") # force a garbage collection gc.collect() assert ref() is None diff --git a/pandas/tests/plotting/frame/test_frame_color.py b/pandas/tests/plotting/frame/test_frame_color.py index 7de3262bfe6cf..3d041fdbb5de6 100644 --- a/pandas/tests/plotting/frame/test_frame_color.py +++ b/pandas/tests/plotting/frame/test_frame_color.py @@ -101,14 +101,12 @@ def test_bar_colors(self): df = DataFrame(np.random.randn(5, 5)) ax = df.plot.bar() _check_colors(ax.patches[::5], facecolors=default_colors[:5]) - tm.close() def test_bar_colors_custom(self): custom_colors = "rgcby" df = DataFrame(np.random.randn(5, 5)) ax = df.plot.bar(color=custom_colors) _check_colors(ax.patches[::5], facecolors=custom_colors) - tm.close() @pytest.mark.parametrize("colormap", ["jet", cm.jet]) def test_bar_colors_cmap(self, colormap): @@ -117,19 +115,16 @@ def test_bar_colors_cmap(self, colormap): ax = df.plot.bar(colormap=colormap) rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, 5)] _check_colors(ax.patches[::5], facecolors=rgba_colors) - tm.close() def test_bar_colors_single_col(self): df = DataFrame(np.random.randn(5, 5)) ax = df.loc[:, [0]].plot.bar(color="DodgerBlue") _check_colors([ax.patches[0]], facecolors=["DodgerBlue"]) - tm.close() def test_bar_colors_green(self): df = DataFrame(np.random.randn(5, 5)) ax = df.plot(kind="bar", color="green") _check_colors(ax.patches[::5], facecolors=["green"] * 5) - tm.close() def test_bar_user_colors(self): df = DataFrame( @@ -254,7 +249,7 @@ def test_line_colors(self): ax = df.plot(color=custom_colors) _check_colors(ax.get_lines(), linecolors=custom_colors) - tm.close() + plt.close("all") ax2 = df.plot(color=custom_colors) lines2 = ax2.get_lines() @@ -262,15 +257,12 @@ def test_line_colors(self): for l1, l2 in zip(ax.get_lines(), lines2): assert l1.get_color() == l2.get_color() - tm.close() - @pytest.mark.parametrize("colormap", ["jet", cm.jet]) def test_line_colors_cmap(self, colormap): df = DataFrame(np.random.randn(5, 5)) ax = df.plot(colormap=colormap) rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))] _check_colors(ax.get_lines(), linecolors=rgba_colors) - tm.close() def test_line_colors_single_col(self): df = DataFrame(np.random.randn(5, 5)) @@ -283,7 +275,6 @@ def test_line_colors_single_color(self): df = DataFrame(np.random.randn(5, 5)) ax = df.plot(color="red") _check_colors(ax.get_lines(), linecolors=["red"] * 5) - tm.close() def test_line_colors_hex(self): # GH 10299 @@ -291,7 +282,6 @@ def test_line_colors_hex(self): custom_colors = ["#FF0000", "#0000FF", "#FFFF00", "#000000", "#FFFFFF"] ax = df.plot(color=custom_colors) _check_colors(ax.get_lines(), linecolors=custom_colors) - tm.close() def test_dont_modify_colors(self): colors = ["r", "g", "b"] @@ -307,7 +297,6 @@ def test_line_colors_and_styles_subplots(self): axes = df.plot(subplots=True) for ax, c in zip(axes, list(default_colors)): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() @pytest.mark.parametrize("color", ["k", "green"]) def test_line_colors_and_styles_subplots_single_color_str(self, color): @@ -315,7 +304,6 @@ def test_line_colors_and_styles_subplots_single_color_str(self, color): axes = df.plot(subplots=True, color=color) for ax in axes: _check_colors(ax.get_lines(), linecolors=[color]) - tm.close() @pytest.mark.parametrize("color", ["rgcby", list("rgcby")]) def test_line_colors_and_styles_subplots_custom_colors(self, color): @@ -324,7 +312,6 @@ def test_line_colors_and_styles_subplots_custom_colors(self, color): axes = df.plot(color=color, subplots=True) for ax, c in zip(axes, list(color)): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() def test_line_colors_and_styles_subplots_colormap_hex(self): # GH 9894 @@ -334,7 +321,6 @@ def test_line_colors_and_styles_subplots_colormap_hex(self): axes = df.plot(color=custom_colors, subplots=True) for ax, c in zip(axes, list(custom_colors)): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() @pytest.mark.parametrize("cmap", ["jet", cm.jet]) def test_line_colors_and_styles_subplots_colormap_subplot(self, cmap): @@ -344,7 +330,6 @@ def test_line_colors_and_styles_subplots_colormap_subplot(self, cmap): axes = df.plot(colormap=cmap, subplots=True) for ax, c in zip(axes, rgba_colors): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() def test_line_colors_and_styles_subplots_single_col(self): # GH 9894 @@ -361,7 +346,6 @@ def test_line_colors_and_styles_subplots_single_char(self): axes = df.plot(style="r", subplots=True) for ax in axes: _check_colors(ax.get_lines(), linecolors=["r"]) - tm.close() def test_line_colors_and_styles_subplots_list_styles(self): # GH 9894 @@ -371,7 +355,6 @@ def test_line_colors_and_styles_subplots_list_styles(self): axes = df.plot(style=styles, subplots=True) for ax, c in zip(axes, styles): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() def test_area_colors(self): from matplotlib.collections import PolyCollection @@ -389,7 +372,6 @@ def test_area_colors(self): for h in handles: assert h.get_alpha() is None - tm.close() def test_area_colors_poly(self): from matplotlib import cm @@ -406,7 +388,6 @@ def test_area_colors_poly(self): _check_colors(handles, facecolors=jet_colors) for h in handles: assert h.get_alpha() is None - tm.close() def test_area_colors_stacked_false(self): from matplotlib import cm @@ -433,14 +414,12 @@ def test_hist_colors(self): df = DataFrame(np.random.randn(5, 5)) ax = df.plot.hist() _check_colors(ax.patches[::10], facecolors=default_colors[:5]) - tm.close() def test_hist_colors_single_custom(self): df = DataFrame(np.random.randn(5, 5)) custom_colors = "rgcby" ax = df.plot.hist(color=custom_colors) _check_colors(ax.patches[::10], facecolors=custom_colors) - tm.close() @pytest.mark.parametrize("colormap", ["jet", cm.jet]) def test_hist_colors_cmap(self, colormap): @@ -448,7 +427,6 @@ def test_hist_colors_cmap(self, colormap): ax = df.plot.hist(colormap=colormap) rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, 5)] _check_colors(ax.patches[::10], facecolors=rgba_colors) - tm.close() def test_hist_colors_single_col(self): df = DataFrame(np.random.randn(5, 5)) @@ -459,7 +437,6 @@ def test_hist_colors_single_color(self): df = DataFrame(np.random.randn(5, 5)) ax = df.plot(kind="hist", color="green") _check_colors(ax.patches[::10], facecolors=["green"] * 5) - tm.close() @td.skip_if_no_scipy def test_kde_colors(self): @@ -468,7 +445,6 @@ def test_kde_colors(self): ax = df.plot.kde(color=custom_colors) _check_colors(ax.get_lines(), linecolors=custom_colors) - tm.close() @td.skip_if_no_scipy @pytest.mark.parametrize("colormap", ["jet", cm.jet]) @@ -477,7 +453,6 @@ def test_kde_colors_cmap(self, colormap): ax = df.plot.kde(colormap=colormap) rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))] _check_colors(ax.get_lines(), linecolors=rgba_colors) - tm.close() @td.skip_if_no_scipy def test_kde_colors_and_styles_subplots(self): @@ -488,7 +463,6 @@ def test_kde_colors_and_styles_subplots(self): axes = df.plot(kind="kde", subplots=True) for ax, c in zip(axes, list(default_colors)): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() @td.skip_if_no_scipy @pytest.mark.parametrize("colormap", ["k", "red"]) @@ -497,7 +471,6 @@ def test_kde_colors_and_styles_subplots_single_col_str(self, colormap): axes = df.plot(kind="kde", color=colormap, subplots=True) for ax in axes: _check_colors(ax.get_lines(), linecolors=[colormap]) - tm.close() @td.skip_if_no_scipy def test_kde_colors_and_styles_subplots_custom_color(self): @@ -506,7 +479,6 @@ def test_kde_colors_and_styles_subplots_custom_color(self): axes = df.plot(kind="kde", color=custom_colors, subplots=True) for ax, c in zip(axes, list(custom_colors)): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() @td.skip_if_no_scipy @pytest.mark.parametrize("colormap", ["jet", cm.jet]) @@ -516,7 +488,6 @@ def test_kde_colors_and_styles_subplots_cmap(self, colormap): axes = df.plot(kind="kde", colormap=colormap, subplots=True) for ax, c in zip(axes, rgba_colors): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() @td.skip_if_no_scipy def test_kde_colors_and_styles_subplots_single_col(self): @@ -534,7 +505,6 @@ def test_kde_colors_and_styles_subplots_single_char(self): axes = df.plot(kind="kde", style="r", subplots=True) for ax in axes: _check_colors(ax.get_lines(), linecolors=["r"]) - tm.close() @td.skip_if_no_scipy def test_kde_colors_and_styles_subplots_list(self): @@ -544,7 +514,6 @@ def test_kde_colors_and_styles_subplots_list(self): axes = df.plot(kind="kde", style=styles, subplots=True) for ax, c in zip(axes, styles): _check_colors(ax.get_lines(), linecolors=[c]) - tm.close() def test_boxplot_colors(self): default_colors = _unpack_cycler(mpl.pyplot.rcParams) @@ -558,7 +527,6 @@ def test_boxplot_colors(self): default_colors[2], default_colors[0], ) - tm.close() def test_boxplot_colors_dict_colors(self): df = DataFrame(np.random.randn(5, 5)) @@ -577,7 +545,6 @@ def test_boxplot_colors_dict_colors(self): dict_colors["caps"], "r", ) - tm.close() def test_boxplot_colors_default_color(self): default_colors = _unpack_cycler(mpl.pyplot.rcParams) @@ -586,7 +553,6 @@ def test_boxplot_colors_default_color(self): dict_colors = {"whiskers": "c", "medians": "m"} bp = df.plot.box(color=dict_colors, return_type="dict") _check_colors_box(bp, default_colors[0], "c", "m", default_colors[0]) - tm.close() @pytest.mark.parametrize("colormap", ["jet", cm.jet]) def test_boxplot_colors_cmap(self, colormap): @@ -596,7 +562,6 @@ def test_boxplot_colors_cmap(self, colormap): _check_colors_box( bp, jet_colors[0], jet_colors[0], jet_colors[2], jet_colors[0] ) - tm.close() def test_boxplot_colors_single(self): df = DataFrame(np.random.randn(5, 5)) diff --git a/pandas/tests/plotting/frame/test_frame_subplots.py b/pandas/tests/plotting/frame/test_frame_subplots.py index 2534d8b99515b..69a68c3d0568a 100644 --- a/pandas/tests/plotting/frame/test_frame_subplots.py +++ b/pandas/tests/plotting/frame/test_frame_subplots.py @@ -282,7 +282,6 @@ def test_subplots_multiple_axes(self): assert returned.shape == (3,) assert returned[0].figure is fig _check_axes_shape(axes, axes_num=6, layout=(2, 3)) - tm.close() def test_subplots_multiple_axes_error(self): # GH 5353, 6970, GH 7069 diff --git a/pandas/tests/plotting/test_boxplot_method.py b/pandas/tests/plotting/test_boxplot_method.py index 9a73a394d0466..2ade2bf6de51a 100644 --- a/pandas/tests/plotting/test_boxplot_method.py +++ b/pandas/tests/plotting/test_boxplot_method.py @@ -26,6 +26,7 @@ from pandas.io.formats.printing import pprint_thing mpl = pytest.importorskip("matplotlib") +plt = pytest.importorskip("matplotlib.pyplot") def _check_ax_limits(col, ax): @@ -418,12 +419,11 @@ def test_grouped_plot_fignums(self): res = gb.plot() assert len(mpl.pyplot.get_fignums()) == 2 assert len(res) == 2 - tm.close() + plt.close("all") res = gb.boxplot(return_type="axes") assert len(mpl.pyplot.get_fignums()) == 1 assert len(res) == 2 - tm.close() def test_grouped_plot_fignums_excluded_col(self): n = 10 @@ -433,7 +433,6 @@ def test_grouped_plot_fignums_excluded_col(self): df = DataFrame({"height": height, "weight": weight, "gender": gender}) # now works with GH 5610 as gender is excluded df.groupby("gender").hist() - tm.close() @pytest.mark.slow def test_grouped_box_return_type(self, hist_df): diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 9a2fa7fb61796..af8bcd943765e 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -178,7 +178,6 @@ def check_format_of_first_point(ax, expected_string): daily = Series(1, index=date_range("2014-01-01", periods=3, freq="D")) daily.plot(ax=ax) check_format_of_first_point(ax, "t = 2014-01-01 y = 1.000000") - tm.close() @pytest.mark.parametrize("freq", ["S", "T", "H", "D", "W", "M", "Q", "A"]) def test_line_plot_period_series(self, freq): @@ -893,7 +892,6 @@ def test_from_weekly_resampling(self): tm.assert_numpy_array_equal(xdata, expected_l) else: tm.assert_numpy_array_equal(xdata, expected_h) - tm.close() @pytest.mark.parametrize("kind1, kind2", [("line", "area"), ("area", "line")]) def test_from_resampling_area_line_mixed(self, kind1, kind2): @@ -1002,7 +1000,6 @@ def test_mixed_freq_second_millisecond(self): assert len(ax.get_lines()) == 2 for line in ax.get_lines(): assert PeriodIndex(data=line.get_xdata()).freq == "L" - tm.close() def test_mixed_freq_second_millisecond_low_to_high(self): # GH 7772, GH 7760 diff --git a/pandas/tests/plotting/test_groupby.py b/pandas/tests/plotting/test_groupby.py index 64c413a054ad0..bf59558d10f24 100644 --- a/pandas/tests/plotting/test_groupby.py +++ b/pandas/tests/plotting/test_groupby.py @@ -9,7 +9,6 @@ Index, Series, ) -import pandas._testing as tm from pandas.tests.plotting.common import ( _check_axes_shape, _check_legend_labels, @@ -25,14 +24,12 @@ def test_series_groupby_plotting_nominally_works(self): gender = np.random.RandomState(42).choice(["male", "female"], size=n) weight.groupby(gender).plot() - tm.close() def test_series_groupby_plotting_nominally_works_hist(self): n = 10 height = Series(np.random.normal(60, 10, size=n)) gender = np.random.RandomState(42).choice(["male", "female"], size=n) height.groupby(gender).hist() - tm.close() def test_series_groupby_plotting_nominally_works_alpha(self): n = 10 @@ -40,7 +37,6 @@ def test_series_groupby_plotting_nominally_works_alpha(self): gender = np.random.RandomState(42).choice(["male", "female"], size=n) # Regression test for GH8733 height.groupby(gender).plot(alpha=0.5) - tm.close() def test_plotting_with_float_index_works(self): # GH 7025 @@ -50,7 +46,6 @@ def test_plotting_with_float_index_works(self): ) df.groupby("def")["val"].plot() - tm.close() def test_plotting_with_float_index_works_apply(self): # GH 7025 @@ -59,7 +54,6 @@ def test_plotting_with_float_index_works_apply(self): index=[1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0], ) df.groupby("def")["val"].apply(lambda x: x.plot()) - tm.close() def test_hist_single_row(self): # GH10214 @@ -76,12 +70,10 @@ def test_hist_single_row_single_bycol(self): def test_plot_submethod_works(self): df = DataFrame({"x": [1, 2, 3, 4, 5], "y": [1, 2, 3, 2, 1], "z": list("ababa")}) df.groupby("z").plot.scatter("x", "y") - tm.close() def test_plot_submethod_works_line(self): df = DataFrame({"x": [1, 2, 3, 4, 5], "y": [1, 2, 3, 2, 1], "z": list("ababa")}) df.groupby("z")["x"].plot.line() - tm.close() def test_plot_kwargs(self): df = DataFrame({"x": [1, 2, 3, 4, 5], "y": [1, 2, 3, 2, 1], "z": list("ababa")}) diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index a153e5dc8f932..77cc2f38dc6f0 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -440,8 +440,6 @@ def test_tight_layout(self): _check_plot_works(df.hist, default_axes=True) mpl.pyplot.tight_layout() - tm.close() - def test_hist_subplot_xrot(self): # GH 30288 df = DataFrame( @@ -582,7 +580,6 @@ def test_hist_secondary_legend(self): _check_legend_labels(ax, labels=["a", "b (right)"]) assert ax.get_yaxis().get_visible() assert ax.right_ax.get_yaxis().get_visible() - tm.close() def test_hist_secondary_secondary(self): # GH 9610 @@ -596,7 +593,6 @@ def test_hist_secondary_secondary(self): _check_legend_labels(ax.left_ax, labels=["a (right)", "b (right)"]) assert not ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() - tm.close() def test_hist_secondary_primary(self): # GH 9610 @@ -611,7 +607,6 @@ def test_hist_secondary_primary(self): _check_legend_labels(ax.left_ax, labels=["a (right)", "b"]) assert ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() - tm.close() @td.skip_if_no_mpl def test_hist_with_nans_and_weights(self): diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index 8186fcc9300e5..2e9283fdf30cd 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -135,7 +135,6 @@ def test_ts_line_lim(self, ts, kwargs): lines = ax.get_lines() assert xmin <= lines[0].get_data(orig=False)[0][0] assert xmax >= lines[0].get_data(orig=False)[0][-1] - tm.close() def test_ts_area_lim(self, ts): _, ax = mpl.pyplot.subplots() @@ -145,7 +144,6 @@ def test_ts_area_lim(self, ts): assert xmin <= line[0] assert xmax >= line[-1] _check_ticks_props(ax, xrot=0) - tm.close() def test_ts_area_lim_xcompat(self, ts): # GH 7471 @@ -156,7 +154,6 @@ def test_ts_area_lim_xcompat(self, ts): assert xmin <= line[0] assert xmax >= line[-1] _check_ticks_props(ax, xrot=30) - tm.close() def test_ts_tz_area_lim_xcompat(self, ts): tz_ts = ts.copy() @@ -168,7 +165,6 @@ def test_ts_tz_area_lim_xcompat(self, ts): assert xmin <= line[0] assert xmax >= line[-1] _check_ticks_props(ax, xrot=0) - tm.close() def test_ts_tz_area_lim_xcompat_secondary_y(self, ts): tz_ts = ts.copy() @@ -180,7 +176,6 @@ def test_ts_tz_area_lim_xcompat_secondary_y(self, ts): assert xmin <= line[0] assert xmax >= line[-1] _check_ticks_props(ax, xrot=0) - tm.close() def test_area_sharey_dont_overwrite(self, ts): # GH37942 @@ -288,7 +283,6 @@ def test_bar_log(self, axis, meth): _, ax = mpl.pyplot.subplots() ax = getattr(Series([200, 500]).plot, meth)(log=True, ax=ax) tm.assert_numpy_array_equal(getattr(ax, axis).get_ticklocs(), expected) - tm.close() @pytest.mark.xfail( np_version_gte1p24 and is_platform_linux(), @@ -311,7 +305,6 @@ def test_bar_log_kind_bar(self, axis, kind, res_meth): tm.assert_almost_equal(res[0], ymin) tm.assert_almost_equal(res[1], ymax) tm.assert_numpy_array_equal(getattr(ax, axis).get_ticklocs(), expected) - tm.close() def test_bar_ignore_index(self): df = Series([1, 2, 3, 4], index=["a", "b", "c", "d"]) @@ -455,7 +448,6 @@ def test_df_series_secondary_legend(self): _check_legend_labels(ax, labels=["a", "b", "c", "x (right)"]) assert ax.get_yaxis().get_visible() assert ax.right_ax.get_yaxis().get_visible() - tm.close() def test_df_series_secondary_legend_with_axes(self): # GH 9779 @@ -470,7 +462,6 @@ def test_df_series_secondary_legend_with_axes(self): _check_legend_labels(ax, labels=["a", "b", "c", "x (right)"]) assert ax.get_yaxis().get_visible() assert ax.right_ax.get_yaxis().get_visible() - tm.close() def test_df_series_secondary_legend_both(self): # GH 9779 @@ -486,7 +477,6 @@ def test_df_series_secondary_legend_both(self): _check_legend_labels(ax.left_ax, labels=expected) assert not ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() - tm.close() def test_df_series_secondary_legend_both_with_axis(self): # GH 9779 @@ -502,7 +492,6 @@ def test_df_series_secondary_legend_both_with_axis(self): _check_legend_labels(ax.left_ax, expected) assert not ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() - tm.close() def test_df_series_secondary_legend_both_with_axis_2(self): # GH 9779 @@ -518,7 +507,6 @@ def test_df_series_secondary_legend_both_with_axis_2(self): _check_legend_labels(ax.left_ax, expected) assert not ax.left_ax.get_yaxis().get_visible() assert ax.get_yaxis().get_visible() - tm.close() @pytest.mark.parametrize( "input_logy, expected_scale", [(True, "log"), ("sym", "symlog")] @@ -666,8 +654,6 @@ def test_errorbar_asymmetrical(self): with pytest.raises(ValueError, match=msg): s.plot(yerr=np.random.rand(2, 11)) - tm.close() - @pytest.mark.slow @pytest.mark.parametrize("kind", ["line", "bar"]) @pytest.mark.parametrize( From 344f7bc2b7ff29963a6a3a936cd78fa5044a94e0 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 29 Jun 2023 11:27:25 -0700 Subject: [PATCH 3/4] trigger ci From 6ba327b0c2a42ff1035b1ff16659e713837eff63 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:22:15 -0700 Subject: [PATCH 4/4] Bump to 50 --- pandas/tests/plotting/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/plotting/conftest.py b/pandas/tests/plotting/conftest.py index 441649168bed6..ae98877dec2ce 100644 --- a/pandas/tests/plotting/conftest.py +++ b/pandas/tests/plotting/conftest.py @@ -31,7 +31,7 @@ def mpl_cleanup(): @pytest.fixture def hist_df(): - n = 20 + n = 50 np_random = np.random.RandomState(42) gender = np_random.choice(["Male", "Female"], size=n) classroom = np_random.choice(["A", "B", "C"], size=n)