From 9d4ee1969eaf96e4fb24896c7231160b10e3fe4e Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 18 Nov 2020 18:41:13 +0100 Subject: [PATCH 1/7] BUG: fix sharey overwrite on area plots --- pandas/plotting/_matplotlib/core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 2501d84de4459..8c78eeb170a81 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1338,7 +1338,9 @@ def _plot( def _post_plot_logic(self, ax: "Axes", data): LinePlot._post_plot_logic(self, ax, data) - if self.ylim is None: + is_shared_y = len(list(ax.get_shared_y_axes())) > 0 + # do not override the default axis behaviour in case of shared y axes + if self.ylim is None and not is_shared_y: if (data >= 0).all().all(): ax.set_ylim(0, None) elif (data <= 0).all().all(): From 32ef66317dfc06ba76993b5d5a97bdb1206e5144 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 18 Nov 2020 18:59:54 +0100 Subject: [PATCH 2/7] add entry in what's new --- doc/source/whatsnew/v1.2.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index e00b177f2a2fc..231b5b0c142ee 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -664,6 +664,7 @@ Plotting - Bug in :meth:`Series.plot` and :meth:`DataFrame.plot` was throwing :exc:`ValueError` with a :class:`Series` or :class:`DataFrame` indexed by a :class:`TimedeltaIndex` with a fixed frequency when x-axis lower limit was greater than upper limit (:issue:`37454`) - Bug in :meth:`DataFrameGroupBy.boxplot` when ``subplots=False``, a KeyError would raise (:issue:`16748`) +- Bug in :meth:`DataFrame.plot` was overwriting matplotlib's shared y axes behaviour when no sharey parameter was passed (:issue:`37942`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ From 876278896a6469324a11d72b5b12670f5b1a3bcf Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 18 Nov 2020 19:02:12 +0100 Subject: [PATCH 3/7] remove trailing whitespace --- pandas/plotting/_matplotlib/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 8c78eeb170a81..97fa3f11e9dfb 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1340,7 +1340,7 @@ def _post_plot_logic(self, ax: "Axes", data): is_shared_y = len(list(ax.get_shared_y_axes())) > 0 # do not override the default axis behaviour in case of shared y axes - if self.ylim is None and not is_shared_y: + if self.ylim is None and not is_shared_y: if (data >= 0).all().all(): ax.set_ylim(0, None) elif (data <= 0).all().all(): From 34295c9555075dc3a8a176ee2d7a85a1fc23ac58 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 19 Nov 2020 15:09:54 +0100 Subject: [PATCH 4/7] added tests --- pandas/tests/plotting/frame/test_frame.py | 12 ++++++++++++ pandas/tests/plotting/test_series.py | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 3c43e0b693a1b..1a8d8a7868ed0 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -477,6 +477,18 @@ def test_area_lim(self): ymin, ymax = ax.get_ylim() assert ymax == 0 + def test_area_sharey_dont_overwrite(self): + # GH37942 + df1 = DataFrame(np.random.rand(4, 2), columns=["x", "y"]) + df2 = DataFrame(np.random.rand(4, 2), columns=["x", "y"]) + fig, (ax1, ax2) = self.plt.subplots(1, 2, sharey=True) + + df1.plot(ax=ax1, kind="area") + df2.plot(ax=ax2, kind="area") + + assert ax1._shared_y_axes.joined(ax1, ax2) + assert ax2._shared_y_axes.joined(ax1, ax2) + @pytest.mark.slow def test_bar_linewidth(self): df = DataFrame(np.random.randn(5, 5)) diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index f3289d0573de2..aaaf7842132f3 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -140,6 +140,16 @@ def test_ts_area_lim(self): assert xmax >= line[-1] self._check_ticks_props(ax, xrot=0) + def test_area_sharey_dont_overwrite(self): + # GH37942 + fig, (ax1, ax2) = self.plt.subplots(1, 2, sharey=True) + + self.ts.plot(ax=ax1, kind="area") + self.ts.plot(ax=ax2, kind="area") + + assert ax1._shared_y_axes.joined(ax1, ax2) + assert ax2._shared_y_axes.joined(ax1, ax2) + def test_label(self): s = Series([1, 2]) _, ax = self.plt.subplots() From a62ccd0060a6d88229d7693040e73d0d54449b5f Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 19 Nov 2020 15:12:48 +0100 Subject: [PATCH 5/7] simplify test --- pandas/tests/plotting/frame/test_frame.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 1a8d8a7868ed0..56ac7a477adbb 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -479,12 +479,11 @@ def test_area_lim(self): def test_area_sharey_dont_overwrite(self): # GH37942 - df1 = DataFrame(np.random.rand(4, 2), columns=["x", "y"]) - df2 = DataFrame(np.random.rand(4, 2), columns=["x", "y"]) + df = DataFrame(np.random.rand(4, 2), columns=["x", "y"]) fig, (ax1, ax2) = self.plt.subplots(1, 2, sharey=True) - df1.plot(ax=ax1, kind="area") - df2.plot(ax=ax2, kind="area") + df.plot(ax=ax1, kind="area") + df.plot(ax=ax2, kind="area") assert ax1._shared_y_axes.joined(ax1, ax2) assert ax2._shared_y_axes.joined(ax1, ax2) From 68e8bce49576ee5099e30d1bf87cd6fe14002171 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 19 Nov 2020 15:59:28 +0100 Subject: [PATCH 6/7] make sure the data for area plot is > 0 --- pandas/tests/plotting/test_series.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index aaaf7842132f3..b8dd2ada87506 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -144,8 +144,8 @@ def test_area_sharey_dont_overwrite(self): # GH37942 fig, (ax1, ax2) = self.plt.subplots(1, 2, sharey=True) - self.ts.plot(ax=ax1, kind="area") - self.ts.plot(ax=ax2, kind="area") + abs(self.ts).plot(ax=ax1, kind="area") + abs(self.ts).plot(ax=ax2, kind="area") assert ax1._shared_y_axes.joined(ax1, ax2) assert ax2._shared_y_axes.joined(ax1, ax2) From b246c3a85a4b9d77925cc7c733af008ac991e8c6 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 19 Nov 2020 20:53:25 +0100 Subject: [PATCH 7/7] add Series.plot in the whatsnew --- doc/source/whatsnew/v1.2.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 231b5b0c142ee..5abad99637ebe 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -664,7 +664,7 @@ Plotting - Bug in :meth:`Series.plot` and :meth:`DataFrame.plot` was throwing :exc:`ValueError` with a :class:`Series` or :class:`DataFrame` indexed by a :class:`TimedeltaIndex` with a fixed frequency when x-axis lower limit was greater than upper limit (:issue:`37454`) - Bug in :meth:`DataFrameGroupBy.boxplot` when ``subplots=False``, a KeyError would raise (:issue:`16748`) -- Bug in :meth:`DataFrame.plot` was overwriting matplotlib's shared y axes behaviour when no sharey parameter was passed (:issue:`37942`) +- Bug in :meth:`DataFrame.plot` and :meth:`Series.plot` was overwriting matplotlib's shared y axes behaviour when no sharey parameter was passed (:issue:`37942`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^