Skip to content

Commit 14cc1fe

Browse files
authored
TST: Add dedicated test for plt.savefig (pandas-dev#55876)
TST: Add dedicated plot for plt.savefig
1 parent 7e0d36f commit 14cc1fe

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

pandas/tests/plotting/common.py

-3
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,6 @@ def _check_plot_works(f, default_axes=False, **kwargs):
535535
for ret in gen_plots(f, fig, **kwargs):
536536
tm.assert_is_valid_plot_return_object(ret)
537537

538-
with tm.ensure_clean(return_filelike=True) as path:
539-
plt.savefig(path)
540-
541538
finally:
542539
plt.close(fig)
543540

pandas/tests/plotting/test_datetimelike.py

-3
Original file line numberDiff line numberDiff line change
@@ -1645,9 +1645,6 @@ def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
16451645
ret = f(*args, **kwargs)
16461646
assert ret is not None # TODO: do something more intelligent
16471647

1648-
with tm.ensure_clean(return_filelike=True) as path:
1649-
plt.savefig(path)
1650-
16511648
# GH18439, GH#24088, statsmodels#4772
16521649
with tm.ensure_clean(return_filelike=True) as path:
16531650
pickle.dump(fig, path)

pandas/tests/plotting/test_misc.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" Test cases for misc plot functions """
2+
import os
23

34
import numpy as np
45
import pytest
@@ -10,7 +11,9 @@
1011
Index,
1112
Series,
1213
Timestamp,
14+
date_range,
1315
interval_range,
16+
period_range,
1417
plotting,
1518
)
1619
import pandas._testing as tm
@@ -23,6 +26,7 @@
2326
)
2427

2528
mpl = pytest.importorskip("matplotlib")
29+
plt = pytest.importorskip("matplotlib.pyplot")
2630
cm = pytest.importorskip("matplotlib.cm")
2731

2832

@@ -69,6 +73,30 @@ def test_get_accessor_args():
6973
assert len(kwargs) == 24
7074

7175

76+
@pytest.mark.parametrize("kind", plotting.PlotAccessor._all_kinds)
77+
@pytest.mark.parametrize(
78+
"data", [DataFrame(np.arange(15).reshape(5, 3)), Series(range(5))]
79+
)
80+
@pytest.mark.parametrize(
81+
"index",
82+
[
83+
Index(range(5)),
84+
date_range("2020-01-01", periods=5),
85+
period_range("2020-01-01", periods=5),
86+
],
87+
)
88+
def test_savefig(kind, data, index):
89+
fig, ax = plt.subplots()
90+
data.index = index
91+
kwargs = {}
92+
if kind in ["hexbin", "scatter", "pie"]:
93+
if isinstance(data, Series):
94+
pytest.skip(f"{kind} not supported with Series")
95+
kwargs = {"x": 0, "y": 1}
96+
data.plot(kind=kind, ax=ax, **kwargs)
97+
fig.savefig(os.devnull)
98+
99+
72100
class TestSeriesPlots:
73101
def test_autocorrelation_plot(self):
74102
from pandas.plotting import autocorrelation_plot

0 commit comments

Comments
 (0)