Skip to content

Commit 8488730

Browse files
authored
BUG: fix sharey overwrite on area plots (#37943)
* BUG: fix sharey overwrite on area plots * add entry in what's new * remove trailing whitespace * added tests * simplify test * make sure the data for area plot is > 0 * add Series.plot in the whatsnew
1 parent 1268cd3 commit 8488730

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ Plotting
670670
- Bug in :meth:`Series.plot` and :meth:`DataFrame.plot` was throwing :exc:`ValueError` with a :class:`Series` or :class:`DataFrame`
671671
indexed by a :class:`TimedeltaIndex` with a fixed frequency when x-axis lower limit was greater than upper limit (:issue:`37454`)
672672
- Bug in :meth:`DataFrameGroupBy.boxplot` when ``subplots=False``, a KeyError would raise (:issue:`16748`)
673+
- 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`)
673674

674675
Groupby/resample/rolling
675676
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_matplotlib/core.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,9 @@ def _plot(
13381338
def _post_plot_logic(self, ax: "Axes", data):
13391339
LinePlot._post_plot_logic(self, ax, data)
13401340

1341-
if self.ylim is None:
1341+
is_shared_y = len(list(ax.get_shared_y_axes())) > 0
1342+
# do not override the default axis behaviour in case of shared y axes
1343+
if self.ylim is None and not is_shared_y:
13421344
if (data >= 0).all().all():
13431345
ax.set_ylim(0, None)
13441346
elif (data <= 0).all().all():

pandas/tests/plotting/frame/test_frame.py

+11
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,17 @@ def test_area_lim(self):
477477
ymin, ymax = ax.get_ylim()
478478
assert ymax == 0
479479

480+
def test_area_sharey_dont_overwrite(self):
481+
# GH37942
482+
df = DataFrame(np.random.rand(4, 2), columns=["x", "y"])
483+
fig, (ax1, ax2) = self.plt.subplots(1, 2, sharey=True)
484+
485+
df.plot(ax=ax1, kind="area")
486+
df.plot(ax=ax2, kind="area")
487+
488+
assert ax1._shared_y_axes.joined(ax1, ax2)
489+
assert ax2._shared_y_axes.joined(ax1, ax2)
490+
480491
@pytest.mark.slow
481492
def test_bar_linewidth(self):
482493
df = DataFrame(np.random.randn(5, 5))

pandas/tests/plotting/test_series.py

+10
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ def test_ts_area_lim(self):
140140
assert xmax >= line[-1]
141141
self._check_ticks_props(ax, xrot=0)
142142

143+
def test_area_sharey_dont_overwrite(self):
144+
# GH37942
145+
fig, (ax1, ax2) = self.plt.subplots(1, 2, sharey=True)
146+
147+
abs(self.ts).plot(ax=ax1, kind="area")
148+
abs(self.ts).plot(ax=ax2, kind="area")
149+
150+
assert ax1._shared_y_axes.joined(ax1, ax2)
151+
assert ax2._shared_y_axes.joined(ax1, ax2)
152+
143153
def test_label(self):
144154
s = Series([1, 2])
145155
_, ax = self.plt.subplots()

0 commit comments

Comments
 (0)