Skip to content

TST: Always close figures in plotting tests #53929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
bdate_range,
)
from pandas._testing._io import (
close,
round_trip_localpath,
round_trip_pathlib,
round_trip_pickle,
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 0 additions & 17 deletions pandas/_testing/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,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)
6 changes: 4 additions & 2 deletions pandas/tests/plotting/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down
34 changes: 16 additions & 18 deletions pandas/tests/plotting/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc

import numpy as np
import pytest

Expand All @@ -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 = 50
np_random = np.random.RandomState(42)
gender = np_random.choice(["Male", "Female"], size=n)
classroom = np_random.choice(["A", "B", "C"], size=n)
Expand Down
27 changes: 4 additions & 23 deletions pandas/tests/plotting/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from pandas.io.formats.printing import pprint_thing

mpl = pytest.importorskip("matplotlib")
plt = pytest.importorskip("matplotlib.pyplot")


class TestDataFramePlots:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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):
Expand All @@ -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",
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -1963,15 +1945,14 @@ 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):
axes = df.plot(subplots=True, ax=axes, sharey=True)

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,
Expand Down Expand Up @@ -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
Expand Down
Loading