diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index bdb86d2dd846f..b51ce375a7ed7 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -2658,67 +2658,84 @@ def test_pie_df_nan(self): @pytest.mark.slow def test_errorbar_plot(self): - with warnings.catch_warnings(): - d = {"x": np.arange(12), "y": np.arange(12, 0, -1)} - df = DataFrame(d) - d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4} - df_err = DataFrame(d_err) + d = {"x": np.arange(12), "y": np.arange(12, 0, -1)} + df = DataFrame(d) + d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4} + df_err = DataFrame(d_err) - # check line plots - ax = _check_plot_works(df.plot, yerr=df_err, logy=True) - self._check_has_errorbars(ax, xerr=0, yerr=2) - ax = _check_plot_works(df.plot, yerr=df_err, logx=True, logy=True) - self._check_has_errorbars(ax, xerr=0, yerr=2) - ax = _check_plot_works(df.plot, yerr=df_err, loglog=True) - self._check_has_errorbars(ax, xerr=0, yerr=2) + # check line plots + ax = _check_plot_works(df.plot, yerr=df_err, logy=True) + self._check_has_errorbars(ax, xerr=0, yerr=2) - kinds = ["line", "bar", "barh"] - for kind in kinds: - ax = _check_plot_works(df.plot, yerr=df_err["x"], kind=kind) - self._check_has_errorbars(ax, xerr=0, yerr=2) - ax = _check_plot_works(df.plot, yerr=d_err, kind=kind) - self._check_has_errorbars(ax, xerr=0, yerr=2) - ax = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, kind=kind) - self._check_has_errorbars(ax, xerr=2, yerr=2) - ax = _check_plot_works( - df.plot, yerr=df_err["x"], xerr=df_err["x"], kind=kind - ) - self._check_has_errorbars(ax, xerr=2, yerr=2) - ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind) - self._check_has_errorbars(ax, xerr=2, yerr=2) - - # _check_plot_works adds an ax so catch warning. see GH #13188 - axes = _check_plot_works( - df.plot, yerr=df_err, xerr=df_err, subplots=True, kind=kind - ) - self._check_has_errorbars(axes, xerr=1, yerr=1) - - ax = _check_plot_works( - (df + 1).plot, yerr=df_err, xerr=df_err, kind="bar", log=True - ) - self._check_has_errorbars(ax, xerr=2, yerr=2) + ax = _check_plot_works(df.plot, yerr=df_err, logx=True, logy=True) + self._check_has_errorbars(ax, xerr=0, yerr=2) - # yerr is raw error values - ax = _check_plot_works(df["y"].plot, yerr=np.ones(12) * 0.4) - self._check_has_errorbars(ax, xerr=0, yerr=1) - ax = _check_plot_works(df.plot, yerr=np.ones((2, 12)) * 0.4) + ax = _check_plot_works(df.plot, yerr=df_err, loglog=True) + self._check_has_errorbars(ax, xerr=0, yerr=2) + + ax = _check_plot_works( + (df + 1).plot, yerr=df_err, xerr=df_err, kind="bar", log=True + ) + self._check_has_errorbars(ax, xerr=2, yerr=2) + + # yerr is raw error values + ax = _check_plot_works(df["y"].plot, yerr=np.ones(12) * 0.4) + self._check_has_errorbars(ax, xerr=0, yerr=1) + + ax = _check_plot_works(df.plot, yerr=np.ones((2, 12)) * 0.4) + self._check_has_errorbars(ax, xerr=0, yerr=2) + + # yerr is column name + for yerr in ["yerr", "誤差"]: + s_df = df.copy() + s_df[yerr] = np.ones(12) * 0.2 + + ax = _check_plot_works(s_df.plot, yerr=yerr) self._check_has_errorbars(ax, xerr=0, yerr=2) - # yerr is column name - for yerr in ["yerr", "誤差"]: - s_df = df.copy() - s_df[yerr] = np.ones(12) * 0.2 - ax = _check_plot_works(s_df.plot, yerr=yerr) - self._check_has_errorbars(ax, xerr=0, yerr=2) - ax = _check_plot_works(s_df.plot, y="y", x="x", yerr=yerr) - self._check_has_errorbars(ax, xerr=0, yerr=1) + ax = _check_plot_works(s_df.plot, y="y", x="x", yerr=yerr) + self._check_has_errorbars(ax, xerr=0, yerr=1) - with pytest.raises(ValueError): - df.plot(yerr=np.random.randn(11)) + with pytest.raises(ValueError): + df.plot(yerr=np.random.randn(11)) - df_err = DataFrame({"x": ["zzz"] * 12, "y": ["zzz"] * 12}) - with pytest.raises((ValueError, TypeError)): - df.plot(yerr=df_err) + df_err = DataFrame({"x": ["zzz"] * 12, "y": ["zzz"] * 12}) + with pytest.raises((ValueError, TypeError)): + df.plot(yerr=df_err) + + @pytest.mark.slow + @pytest.mark.parametrize("kind", ["line", "bar", "barh"]) + def test_errorbar_plot_different_kinds(self, kind): + d = {"x": np.arange(12), "y": np.arange(12, 0, -1)} + df = DataFrame(d) + d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4} + df_err = DataFrame(d_err) + + ax = _check_plot_works(df.plot, yerr=df_err["x"], kind=kind) + self._check_has_errorbars(ax, xerr=0, yerr=2) + + ax = _check_plot_works(df.plot, yerr=d_err, kind=kind) + self._check_has_errorbars(ax, xerr=0, yerr=2) + + ax = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, kind=kind) + self._check_has_errorbars(ax, xerr=2, yerr=2) + + ax = _check_plot_works(df.plot, yerr=df_err["x"], xerr=df_err["x"], kind=kind) + self._check_has_errorbars(ax, xerr=2, yerr=2) + + ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind) + self._check_has_errorbars(ax, xerr=2, yerr=2) + + with tm.assert_produces_warning(UserWarning): + # _check_plot_works creates subplots inside, + # which leads to warnings like this: + # UserWarning: To output multiple subplots, + # the figure containing the passed axes is being cleared + # Similar warnings were observed in GH #13188 + axes = _check_plot_works( + df.plot, yerr=df_err, xerr=df_err, subplots=True, kind=kind + ) + self._check_has_errorbars(axes, xerr=1, yerr=1) @pytest.mark.xfail(reason="Iterator is consumed", raises=ValueError) @pytest.mark.slow @@ -2765,35 +2782,39 @@ def test_errorbar_with_partial_columns(self): self._check_has_errorbars(ax, xerr=0, yerr=1) @pytest.mark.slow - def test_errorbar_timeseries(self): + @pytest.mark.parametrize("kind", ["line", "bar", "barh"]) + def test_errorbar_timeseries(self, kind): + d = {"x": np.arange(12), "y": np.arange(12, 0, -1)} + d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4} - with warnings.catch_warnings(): - d = {"x": np.arange(12), "y": np.arange(12, 0, -1)} - d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4} + # check time-series plots + ix = date_range("1/1/2000", "1/1/2001", freq="M") + tdf = DataFrame(d, index=ix) + tdf_err = DataFrame(d_err, index=ix) - # check time-series plots - ix = date_range("1/1/2000", "1/1/2001", freq="M") - tdf = DataFrame(d, index=ix) - tdf_err = DataFrame(d_err, index=ix) + ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind) + self._check_has_errorbars(ax, xerr=0, yerr=2) - kinds = ["line", "bar", "barh"] - for kind in kinds: - ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind) - self._check_has_errorbars(ax, xerr=0, yerr=2) - ax = _check_plot_works(tdf.plot, yerr=d_err, kind=kind) - self._check_has_errorbars(ax, xerr=0, yerr=2) - ax = _check_plot_works(tdf.plot, y="y", yerr=tdf_err["x"], kind=kind) - self._check_has_errorbars(ax, xerr=0, yerr=1) - ax = _check_plot_works(tdf.plot, y="y", yerr="x", kind=kind) - self._check_has_errorbars(ax, xerr=0, yerr=1) - ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind) - self._check_has_errorbars(ax, xerr=0, yerr=2) - - # _check_plot_works adds an ax so catch warning. see GH #13188 - axes = _check_plot_works( - tdf.plot, kind=kind, yerr=tdf_err, subplots=True - ) - self._check_has_errorbars(axes, xerr=0, yerr=1) + ax = _check_plot_works(tdf.plot, yerr=d_err, kind=kind) + self._check_has_errorbars(ax, xerr=0, yerr=2) + + ax = _check_plot_works(tdf.plot, y="y", yerr=tdf_err["x"], kind=kind) + self._check_has_errorbars(ax, xerr=0, yerr=1) + + ax = _check_plot_works(tdf.plot, y="y", yerr="x", kind=kind) + self._check_has_errorbars(ax, xerr=0, yerr=1) + + ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind) + self._check_has_errorbars(ax, xerr=0, yerr=2) + + with tm.assert_produces_warning(UserWarning): + # _check_plot_works creates subplots inside, + # which leads to warnings like this: + # UserWarning: To output multiple subplots, + # the figure containing the passed axes is being cleared + # Similar warnings were observed in GH #13188 + axes = _check_plot_works(tdf.plot, kind=kind, yerr=tdf_err, subplots=True) + self._check_has_errorbars(axes, xerr=0, yerr=1) def test_errorbar_asymmetrical(self):