Skip to content

Commit 0c9ca6b

Browse files
authored
Merge pull request #21906 from meeseeksmachine/auto-backport-of-pr-21773-on-v3.5.x
Backport PR #21773 on branch v3.5.x (FIX: Reset label of axis to center)
2 parents b8a60b9 + 1084483 commit 0c9ca6b

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,15 +3488,19 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *,
34883488
f"its corresponding low level keyword "
34893489
f"arguments ({protected_kw}) are also "
34903490
f"supplied")
3491-
loc = 'center'
3491+
34923492
else:
34933493
loc = (loc if loc is not None
34943494
else mpl.rcParams['xaxis.labellocation'])
3495-
_api.check_in_list(('left', 'center', 'right'), loc=loc)
3496-
if loc == 'left':
3497-
kwargs.update(x=0, horizontalalignment='left')
3498-
elif loc == 'right':
3499-
kwargs.update(x=1, horizontalalignment='right')
3495+
_api.check_in_list(('left', 'center', 'right'), loc=loc)
3496+
3497+
if loc == 'left':
3498+
kwargs.update(x=0, horizontalalignment='left')
3499+
elif loc == 'center':
3500+
kwargs.update(x=0.5, horizontalalignment='center')
3501+
elif loc == 'right':
3502+
kwargs.update(x=1, horizontalalignment='right')
3503+
35003504
return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
35013505

35023506
def invert_xaxis(self):
@@ -3832,15 +3836,19 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *,
38323836
f"its corresponding low level keyword "
38333837
f"arguments ({protected_kw}) are also "
38343838
f"supplied")
3835-
loc = 'center'
3839+
38363840
else:
38373841
loc = (loc if loc is not None
38383842
else mpl.rcParams['yaxis.labellocation'])
3839-
_api.check_in_list(('bottom', 'center', 'top'), loc=loc)
3840-
if loc == 'bottom':
3841-
kwargs.update(y=0, horizontalalignment='left')
3842-
elif loc == 'top':
3843-
kwargs.update(y=1, horizontalalignment='right')
3843+
_api.check_in_list(('bottom', 'center', 'top'), loc=loc)
3844+
3845+
if loc == 'bottom':
3846+
kwargs.update(y=0, horizontalalignment='left')
3847+
elif loc == 'center':
3848+
kwargs.update(y=0.5, horizontalalignment='center')
3849+
elif loc == 'top':
3850+
kwargs.update(y=1, horizontalalignment='right')
3851+
38443852
return self.yaxis.set_label_text(ylabel, fontdict, **kwargs)
38453853

38463854
def invert_yaxis(self):

lib/matplotlib/tests/test_axes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ def test_label_loc_rc(fig_test, fig_ref):
104104
cbar.set_label("Z Label", x=1, ha='right')
105105

106106

107+
def test_label_shift():
108+
fig, ax = plt.subplots()
109+
110+
# Test label re-centering on x-axis
111+
ax.set_xlabel("Test label", loc="left")
112+
ax.set_xlabel("Test label", loc="center")
113+
assert ax.xaxis.get_label().get_horizontalalignment() == "center"
114+
ax.set_xlabel("Test label", loc="right")
115+
assert ax.xaxis.get_label().get_horizontalalignment() == "right"
116+
ax.set_xlabel("Test label", loc="center")
117+
assert ax.xaxis.get_label().get_horizontalalignment() == "center"
118+
119+
# Test label re-centering on y-axis
120+
ax.set_ylabel("Test label", loc="top")
121+
ax.set_ylabel("Test label", loc="center")
122+
assert ax.yaxis.get_label().get_horizontalalignment() == "center"
123+
ax.set_ylabel("Test label", loc="bottom")
124+
assert ax.yaxis.get_label().get_horizontalalignment() == "left"
125+
ax.set_ylabel("Test label", loc="center")
126+
assert ax.yaxis.get_label().get_horizontalalignment() == "center"
127+
128+
107129
@check_figures_equal(extensions=["png"])
108130
def test_acorr(fig_test, fig_ref):
109131
np.random.seed(19680801)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy