Skip to content

Commit f9346a6

Browse files
authored
BUG: Set y-axis label, limits and ticks for a secondary y-axis (#47753) (#47754)
When passing secondary_y=True to a plotting function, a second axes with a y-axis on the right side is created. Passing ylabel, ylim or yticks changed these properties of the original invisible left y-axis, not the secondary y-axis.
1 parent 87930ef commit f9346a6

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,7 @@ Plotting
978978
- The function :meth:`DataFrame.plot.scatter` now accepts ``color`` as an alias for ``c`` and ``size`` as an alias for ``s`` for consistency to other plotting functions (:issue:`44670`)
979979
- Fix showing "None" as ylabel in :meth:`Series.plot` when not setting ylabel (:issue:`46129`)
980980
- Bug in :meth:`DataFrame.plot` that led to xticks and vertical grids being improperly placed when plotting a quarterly series (:issue:`47602`)
981+
- Bug in :meth:`DataFrame.plot` that prevented setting y-axis label, limits and ticks for a secondary y-axis (:issue:`47753`)
981982

982983
Groupby/resample/rolling
983984
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_matplotlib/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ def _adorn_subplots(self):
679679
)
680680

681681
for ax in self.axes:
682+
ax = getattr(ax, "right_ax", ax)
682683
if self.yticks is not None:
683684
ax.set_yticks(self.yticks)
684685

pandas/tests/plotting/frame/test_frame.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,17 @@ def test_xlabel_ylabel_dataframe_plane_plot(self, kind, xlabel, ylabel):
22042204
assert ax.get_xlabel() == (xcol if xlabel is None else xlabel)
22052205
assert ax.get_ylabel() == (ycol if ylabel is None else ylabel)
22062206

2207+
@pytest.mark.parametrize("secondary_y", (False, True))
2208+
def test_secondary_y(self, secondary_y):
2209+
ax_df = DataFrame([0]).plot(
2210+
secondary_y=secondary_y, ylabel="Y", ylim=(0, 100), yticks=[99]
2211+
)
2212+
for ax in ax_df.figure.axes:
2213+
if ax.yaxis.get_visible():
2214+
assert ax.get_ylabel() == "Y"
2215+
assert ax.get_ylim() == (0, 100)
2216+
assert ax.get_yticks()[0] == 99
2217+
22072218

22082219
def _generate_4_axes_via_gridspec():
22092220
import matplotlib as mpl

0 commit comments

Comments
 (0)