Skip to content

TST: Ensure Matplotlib is always cleaned up #57389

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 1 commit into from
Feb 13, 2024
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
34 changes: 34 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
timezone,
)
from decimal import Decimal
import gc
import operator
import os
from typing import (
Expand Down Expand Up @@ -1863,6 +1864,39 @@ def ip():
return InteractiveShell(config=c)


@pytest.fixture
def mpl_cleanup():
"""
Ensure Matplotlib is cleaned up around a test.

Before a test is run:

1) Set the backend to "template" to avoid requiring a GUI.

After a test is run:

1) Reset units registry
2) Reset rc_context
3) Close all figures

See matplotlib/testing/decorators.py#L24.
"""
mpl = pytest.importorskip("matplotlib")
mpl_units = pytest.importorskip("matplotlib.units")
plt = pytest.importorskip("matplotlib.pyplot")
orig_units_registry = mpl_units.registry.copy()
try:
with mpl.rc_context():
mpl.use("template")
yield
finally:
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(
params=[
getattr(pd.offsets, o)
Expand Down
22 changes: 1 addition & 21 deletions pandas/tests/io/formats/style/test_matplotlib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import gc

import numpy as np
import pytest

Expand All @@ -16,25 +14,7 @@

from pandas.io.formats.style import Styler


@pytest.fixture(autouse=True)
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
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)
pytestmark = pytest.mark.usefixtures("mpl_cleanup")


@pytest.fixture
Expand Down
21 changes: 2 additions & 19 deletions pandas/tests/plotting/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import gc

import numpy as np
import pytest

Expand All @@ -10,23 +8,8 @@


@pytest.fixture(autouse=True)
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
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)
def autouse_mpl_cleanup(mpl_cleanup):
pass


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_scikit_learn():
clf.predict(digits.data[-1:])


def test_seaborn():
def test_seaborn(mpl_cleanup):
seaborn = pytest.importorskip("seaborn")
tips = DataFrame(
{"day": pd.date_range("2023", freq="D", periods=5), "total_bill": range(5)}
Expand Down