Skip to content

Commit 73bf910

Browse files
committed
REF: catch warning in test_errorbar_plot
1 parent 4083380 commit 73bf910

File tree

1 file changed

+66
-50
lines changed

1 file changed

+66
-50
lines changed

pandas/tests/plotting/test_frame.py

+66-50
Original file line numberDiff line numberDiff line change
@@ -2658,67 +2658,83 @@ def test_pie_df_nan(self):
26582658

26592659
@pytest.mark.slow
26602660
def test_errorbar_plot(self):
2661-
with warnings.catch_warnings():
2662-
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2663-
df = DataFrame(d)
2664-
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
2665-
df_err = DataFrame(d_err)
2661+
import matplotlib.pyplot as plt
26662662

2667-
# check line plots
2668-
ax = _check_plot_works(df.plot, yerr=df_err, logy=True)
2669-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2670-
ax = _check_plot_works(df.plot, yerr=df_err, logx=True, logy=True)
2671-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2672-
ax = _check_plot_works(df.plot, yerr=df_err, loglog=True)
2663+
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2664+
df = DataFrame(d)
2665+
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
2666+
df_err = DataFrame(d_err)
2667+
2668+
# check line plots
2669+
ax = _check_plot_works(df.plot, yerr=df_err, logy=True)
2670+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2671+
2672+
ax = _check_plot_works(df.plot, yerr=df_err, logx=True, logy=True)
2673+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2674+
2675+
ax = _check_plot_works(df.plot, yerr=df_err, loglog=True)
2676+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2677+
2678+
kinds = ["line", "bar", "barh"]
2679+
for kind in kinds:
2680+
ax = _check_plot_works(df.plot, yerr=df_err["x"], kind=kind)
26732681
self._check_has_errorbars(ax, xerr=0, yerr=2)
26742682

2675-
kinds = ["line", "bar", "barh"]
2676-
for kind in kinds:
2677-
ax = _check_plot_works(df.plot, yerr=df_err["x"], kind=kind)
2678-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2679-
ax = _check_plot_works(df.plot, yerr=d_err, kind=kind)
2680-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2681-
ax = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, kind=kind)
2682-
self._check_has_errorbars(ax, xerr=2, yerr=2)
2683-
ax = _check_plot_works(
2684-
df.plot, yerr=df_err["x"], xerr=df_err["x"], kind=kind
2685-
)
2686-
self._check_has_errorbars(ax, xerr=2, yerr=2)
2687-
ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind)
2688-
self._check_has_errorbars(ax, xerr=2, yerr=2)
2683+
ax = _check_plot_works(df.plot, yerr=d_err, kind=kind)
2684+
self._check_has_errorbars(ax, xerr=0, yerr=2)
26892685

2690-
# _check_plot_works adds an ax so catch warning. see GH #13188
2691-
axes = _check_plot_works(
2692-
df.plot, yerr=df_err, xerr=df_err, subplots=True, kind=kind
2693-
)
2694-
self._check_has_errorbars(axes, xerr=1, yerr=1)
2686+
ax = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, kind=kind)
2687+
self._check_has_errorbars(ax, xerr=2, yerr=2)
26952688

26962689
ax = _check_plot_works(
2697-
(df + 1).plot, yerr=df_err, xerr=df_err, kind="bar", log=True
2690+
df.plot, yerr=df_err["x"], xerr=df_err["x"], kind=kind
26982691
)
26992692
self._check_has_errorbars(ax, xerr=2, yerr=2)
27002693

2701-
# yerr is raw error values
2702-
ax = _check_plot_works(df["y"].plot, yerr=np.ones(12) * 0.4)
2703-
self._check_has_errorbars(ax, xerr=0, yerr=1)
2704-
ax = _check_plot_works(df.plot, yerr=np.ones((2, 12)) * 0.4)
2705-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2694+
ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind)
2695+
self._check_has_errorbars(ax, xerr=2, yerr=2)
27062696

2707-
# yerr is column name
2708-
for yerr in ["yerr", "誤差"]:
2709-
s_df = df.copy()
2710-
s_df[yerr] = np.ones(12) * 0.2
2711-
ax = _check_plot_works(s_df.plot, yerr=yerr)
2712-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2713-
ax = _check_plot_works(s_df.plot, y="y", x="x", yerr=yerr)
2714-
self._check_has_errorbars(ax, xerr=0, yerr=1)
2697+
with tm.assert_produces_warning(UserWarning):
2698+
# _check_plot_works as this function creates
2699+
# subplots inside, which leads to warnings like this:
2700+
# UserWarning: To output multiple subplots,
2701+
# the figure containing the passed axes is being cleared
2702+
# Similar warnings were observed in GH #13188
2703+
_check_plot_works(
2704+
df.plot, yerr=df_err, xerr=df_err, subplots=True, kind=kind
2705+
)
2706+
fig = plt.gcf()
2707+
axes = fig.get_axes()
2708+
for ax in axes:
2709+
self._check_has_errorbars(ax, xerr=1, yerr=1)
27152710

2716-
with pytest.raises(ValueError):
2717-
df.plot(yerr=np.random.randn(11))
2711+
ax = _check_plot_works(
2712+
(df + 1).plot, yerr=df_err, xerr=df_err, kind="bar", log=True
2713+
)
2714+
self._check_has_errorbars(ax, xerr=2, yerr=2)
2715+
2716+
# yerr is raw error values
2717+
ax = _check_plot_works(df["y"].plot, yerr=np.ones(12) * 0.4)
2718+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2719+
2720+
ax = _check_plot_works(df.plot, yerr=np.ones((2, 12)) * 0.4)
2721+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2722+
2723+
# yerr is column name
2724+
for yerr in ["yerr", "誤差"]:
2725+
s_df = df.copy()
2726+
s_df[yerr] = np.ones(12) * 0.2
2727+
ax = _check_plot_works(s_df.plot, yerr=yerr)
2728+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2729+
ax = _check_plot_works(s_df.plot, y="y", x="x", yerr=yerr)
2730+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2731+
2732+
with pytest.raises(ValueError):
2733+
df.plot(yerr=np.random.randn(11))
27182734

2719-
df_err = DataFrame({"x": ["zzz"] * 12, "y": ["zzz"] * 12})
2720-
with pytest.raises((ValueError, TypeError)):
2721-
df.plot(yerr=df_err)
2735+
df_err = DataFrame({"x": ["zzz"] * 12, "y": ["zzz"] * 12})
2736+
with pytest.raises((ValueError, TypeError)):
2737+
df.plot(yerr=df_err)
27222738

27232739
@pytest.mark.xfail(reason="Iterator is consumed", raises=ValueError)
27242740
@pytest.mark.slow
@@ -2798,7 +2814,7 @@ def test_errorbar_timeseries(self, kind):
27982814
# UserWarning: To output multiple subplots,
27992815
# the figure containing the passed axes is being cleared
28002816
# Similar warnings were observed in GH #13188
2801-
axes = _check_plot_works(tdf.plot, kind=kind, yerr=tdf_err, subplots=True)
2817+
_check_plot_works(tdf.plot, kind=kind, yerr=tdf_err, subplots=True)
28022818
fig = plt.gcf()
28032819
axes = fig.get_axes()
28042820
for ax in axes:

0 commit comments

Comments
 (0)