Skip to content

Commit 9e7abc8

Browse files
authored
CLN: Plotting testing asserters (#58922)
1 parent 92207fe commit 9e7abc8

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

pandas/_testing/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
assert_indexing_slices_equivalent,
5858
assert_interval_array_equal,
5959
assert_is_sorted,
60-
assert_is_valid_plot_return_object,
6160
assert_metadata_equivalent,
6261
assert_numpy_array_equal,
6362
assert_period_array_equal,
@@ -558,7 +557,6 @@ def shares_memory(left, right) -> bool:
558557
"assert_indexing_slices_equivalent",
559558
"assert_interval_array_equal",
560559
"assert_is_sorted",
561-
"assert_is_valid_plot_return_object",
562560
"assert_metadata_equivalent",
563561
"assert_numpy_array_equal",
564562
"assert_period_array_equal",

pandas/_testing/asserters.py

-22
Original file line numberDiff line numberDiff line change
@@ -429,28 +429,6 @@ def assert_attr_equal(attr: str, left, right, obj: str = "Attributes") -> None:
429429
return None
430430

431431

432-
def assert_is_valid_plot_return_object(objs) -> None:
433-
from matplotlib.artist import Artist
434-
from matplotlib.axes import Axes
435-
436-
if isinstance(objs, (Series, np.ndarray)):
437-
if isinstance(objs, Series):
438-
objs = objs._values
439-
for el in objs.ravel():
440-
msg = (
441-
"one of 'objs' is not a matplotlib Axes instance, "
442-
f"type encountered {type(el).__name__!r}"
443-
)
444-
assert isinstance(el, (Axes, dict)), msg
445-
else:
446-
msg = (
447-
"objs is neither an ndarray of Artist instances nor a single "
448-
"ArtistArtist instance, tuple, or dict, 'objs' is a "
449-
f"{type(objs).__name__!r}"
450-
)
451-
assert isinstance(objs, (Artist, tuple, dict)), msg
452-
453-
454432
def assert_is_sorted(seq) -> None:
455433
"""Assert that the sequence is sorted."""
456434
if isinstance(seq, (Index, Series)):

pandas/tests/plotting/common.py

+26-12
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ def _check_data(xp, rs):
7676
xp : matplotlib Axes object
7777
rs : matplotlib Axes object
7878
"""
79-
import matplotlib.pyplot as plt
80-
8179
xp_lines = xp.get_lines()
8280
rs_lines = rs.get_lines()
8381

@@ -87,8 +85,6 @@ def _check_data(xp, rs):
8785
rsdata = rsl.get_xydata()
8886
tm.assert_almost_equal(xpdata, rsdata)
8987

90-
plt.close("all")
91-
9288

9389
def _check_visible(collections, visible=True):
9490
"""
@@ -495,6 +491,28 @@ def get_y_axis(ax):
495491
return ax._shared_axes["y"]
496492

497493

494+
def assert_is_valid_plot_return_object(objs) -> None:
495+
from matplotlib.artist import Artist
496+
from matplotlib.axes import Axes
497+
498+
if isinstance(objs, (Series, np.ndarray)):
499+
if isinstance(objs, Series):
500+
objs = objs._values
501+
for el in objs.reshape(-1):
502+
msg = (
503+
"one of 'objs' is not a matplotlib Axes instance, "
504+
f"type encountered {type(el).__name__!r}"
505+
)
506+
assert isinstance(el, (Axes, dict)), msg
507+
else:
508+
msg = (
509+
"objs is neither an ndarray of Artist instances nor a single "
510+
"ArtistArtist instance, tuple, or dict, 'objs' is a "
511+
f"{type(objs).__name__!r}"
512+
)
513+
assert isinstance(objs, (Artist, tuple, dict)), msg
514+
515+
498516
def _check_plot_works(f, default_axes=False, **kwargs):
499517
"""
500518
Create plot and ensure that plot return object is valid.
@@ -530,15 +548,11 @@ def _check_plot_works(f, default_axes=False, **kwargs):
530548
gen_plots = _gen_two_subplots
531549

532550
ret = None
533-
try:
534-
fig = kwargs.get("figure", plt.gcf())
535-
plt.clf()
536-
537-
for ret in gen_plots(f, fig, **kwargs):
538-
tm.assert_is_valid_plot_return_object(ret)
551+
fig = kwargs.get("figure", plt.gcf())
552+
fig.clf()
539553

540-
finally:
541-
plt.close(fig)
554+
for ret in gen_plots(f, fig, **kwargs):
555+
assert_is_valid_plot_return_object(ret)
542556

543557
return ret
544558

0 commit comments

Comments
 (0)