Skip to content

BUG: fix sharey overwrite on area plots #37943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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` and :meth:`Series.plot` was overwriting matplotlib's shared y axes behaviour when no sharey parameter was passed (:issue:`37942`)

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/plotting/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,17 @@ def test_area_lim(self):
ymin, ymax = ax.get_ylim()
assert ymax == 0

def test_area_sharey_dont_overwrite(self):
# GH37942
df = DataFrame(np.random.rand(4, 2), columns=["x", "y"])
fig, (ax1, ax2) = self.plt.subplots(1, 2, sharey=True)

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)

@pytest.mark.slow
def test_bar_linewidth(self):
df = DataFrame(np.random.randn(5, 5))
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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)

def test_label(self):
s = Series([1, 2])
_, ax = self.plt.subplots()
Expand Down