Skip to content

Commit 1c04f3c

Browse files
committed
FIX: fix cleanup warnings for errorbar timeseries
1 parent 257ad4e commit 1c04f3c

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

pandas/tests/plotting/test_frame.py

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,35 +2765,62 @@ def test_errorbar_with_partial_columns(self):
27652765
self._check_has_errorbars(ax, xerr=0, yerr=1)
27662766

27672767
@pytest.mark.slow
2768-
def test_errorbar_timeseries(self):
2768+
@pytest.mark.parametrize("kind", ["line", "bar", "barh"])
2769+
def test_errorbar_timeseries(self, kind):
2770+
import matplotlib.pyplot as plt
27692771

2770-
with warnings.catch_warnings():
2771-
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2772-
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
2772+
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2773+
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
27732774

2774-
# check time-series plots
2775-
ix = date_range("1/1/2000", "1/1/2001", freq="M")
2776-
tdf = DataFrame(d, index=ix)
2777-
tdf_err = DataFrame(d_err, index=ix)
2775+
# check time-series plots
2776+
ix = date_range("1/1/2000", "1/1/2001", freq="M")
2777+
tdf = DataFrame(d, index=ix)
2778+
tdf_err = DataFrame(d_err, index=ix)
27782779

2779-
kinds = ["line", "bar", "barh"]
2780-
for kind in kinds:
2781-
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
2782-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2783-
ax = _check_plot_works(tdf.plot, yerr=d_err, kind=kind)
2784-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2785-
ax = _check_plot_works(tdf.plot, y="y", yerr=tdf_err["x"], kind=kind)
2786-
self._check_has_errorbars(ax, xerr=0, yerr=1)
2787-
ax = _check_plot_works(tdf.plot, y="y", yerr="x", kind=kind)
2788-
self._check_has_errorbars(ax, xerr=0, yerr=1)
2789-
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
2790-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2780+
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
2781+
self._check_has_errorbars(ax, xerr=0, yerr=2)
27912782

2792-
# _check_plot_works adds an ax so catch warning. see GH #13188
2793-
axes = _check_plot_works(
2794-
tdf.plot, kind=kind, yerr=tdf_err, subplots=True
2795-
)
2796-
self._check_has_errorbars(axes, xerr=0, yerr=1)
2783+
ax = _check_plot_works(tdf.plot, yerr=d_err, kind=kind)
2784+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2785+
2786+
ax = _check_plot_works(tdf.plot, y="y", yerr=tdf_err["x"], kind=kind)
2787+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2788+
2789+
ax = _check_plot_works(tdf.plot, y="y", yerr="x", kind=kind)
2790+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2791+
2792+
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
2793+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2794+
2795+
# Do not use _check_plot_works as this function creates
2796+
# subplots inside, which led to uncaught warnings like this:
2797+
# UserWarning: To output multiple subplots,
2798+
# the figure containing the passed axes is being cleared
2799+
tdf.plot(kind=kind, yerr=tdf_err, subplots=True)
2800+
fig = plt.gcf()
2801+
axes = fig.get_axes()
2802+
for ax in axes:
2803+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2804+
2805+
@pytest.mark.slow
2806+
@pytest.mark.parametrize("kind", ["line", "bar", "barh"])
2807+
def test_errorbar_plotting_twice_on_same_axis_warns(self, kind):
2808+
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2809+
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
2810+
2811+
ix = date_range("1/1/2000", "1/1/2001", freq="M")
2812+
tdf = DataFrame(d, index=ix)
2813+
tdf_err = DataFrame(d_err, index=ix)
2814+
2815+
with warnings.catch_warnings(record=True) as w:
2816+
# _check_plot_works creates subplots inside,
2817+
# thus the warning about overwriting must be raised
2818+
axes = _check_plot_works(tdf.plot, kind=kind, yerr=tdf_err, subplots=True)
2819+
self._check_has_errorbars(axes, xerr=0, yerr=1)
2820+
assert len(w) == 1
2821+
assert issubclass(w[0].category, UserWarning)
2822+
msg = "the figure containing the passed axes is being cleared"
2823+
assert msg in str(w[0].message)
27972824

27982825
def test_errorbar_asymmetrical(self):
27992826

0 commit comments

Comments
 (0)