Skip to content

Commit 8e3d831

Browse files
Morgan243gfyoung
authored andcommitted
BUG: Set secondary axis font size for secondary_y during plotting
The parameter was not being respected for `secondary_y`. Closes gh-12565
1 parent 2cd85ca commit 8e3d831

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ I/O
166166

167167
Plotting
168168
^^^^^^^^
169-
169+
- Bug in plotting methods using ``secondary_y`` and ``fontsize`` not setting secondary axis font size (:issue:`12565`)
170170

171171

172172
Groupby/Resample/Rolling

pandas/plotting/_core.py

+9
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,22 @@ def _post_plot_logic_common(self, ax, data):
379379
self._apply_axis_properties(ax.xaxis, rot=self.rot,
380380
fontsize=self.fontsize)
381381
self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
382+
383+
if hasattr(ax, 'right_ax'):
384+
self._apply_axis_properties(ax.right_ax.yaxis,
385+
fontsize=self.fontsize)
386+
382387
elif self.orientation == 'horizontal':
383388
if self._need_to_set_index:
384389
yticklabels = [labels.get(y, '') for y in ax.get_yticks()]
385390
ax.set_yticklabels(yticklabels)
386391
self._apply_axis_properties(ax.yaxis, rot=self.rot,
387392
fontsize=self.fontsize)
388393
self._apply_axis_properties(ax.xaxis, fontsize=self.fontsize)
394+
395+
if hasattr(ax, 'right_ax'):
396+
self._apply_axis_properties(ax.right_ax.yaxis,
397+
fontsize=self.fontsize)
389398
else: # pragma no cover
390399
raise ValueError
391400

pandas/tests/plotting/test_frame.py

+17
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,23 @@ def test_rcParams_bar_colors(self):
27332733
barplot = pd.DataFrame([[1, 2, 3]]).plot(kind="bar")
27342734
assert color_tuples == [c.get_facecolor() for c in barplot.patches]
27352735

2736+
@pytest.mark.parametrize('method', ['line', 'barh', 'bar'])
2737+
def test_secondary_axis_font_size(self, method):
2738+
# GH: 12565
2739+
df = (pd.DataFrame(np.random.randn(15, 2),
2740+
columns=list('AB'))
2741+
.assign(C=lambda df: df.B.cumsum())
2742+
.assign(D=lambda df: df.C * 1.1))
2743+
2744+
fontsize = 20
2745+
sy = ['C', 'D']
2746+
2747+
kwargs = dict(secondary_y=sy, fontsize=fontsize,
2748+
mark_right=True)
2749+
ax = getattr(df.plot, method)(**kwargs)
2750+
self._check_ticks_props(axes=ax.right_ax,
2751+
ylabelsize=fontsize)
2752+
27362753

27372754
def _generate_4_axes_via_gridspec():
27382755
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)