Skip to content

Commit 8f1602b

Browse files
authored
FIX: Reset label of axis to center (matplotlib#21773)
* Fix label shift and add test * Use public api and revert changes * Resolve ha conflict * Allow ha overwrite * Allow ha overwrite * Allox x/y overwrite * Fix bug * Throw aliasing error * Remove plt.close() * Remove redundancy * Call normalize_kwargs() * Use setdefault() * Use loc absent x, ha
1 parent 5bb1449 commit 8f1602b

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
@@ -3487,15 +3487,19 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *,
34873487
f"its corresponding low level keyword "
34883488
f"arguments ({protected_kw}) are also "
34893489
f"supplied")
3490-
loc = 'center'
3490+
34913491
else:
34923492
loc = (loc if loc is not None
34933493
else mpl.rcParams['xaxis.labellocation'])
3494-
_api.check_in_list(('left', 'center', 'right'), loc=loc)
3495-
if loc == 'left':
3496-
kwargs.update(x=0, horizontalalignment='left')
3497-
elif loc == 'right':
3498-
kwargs.update(x=1, horizontalalignment='right')
3494+
_api.check_in_list(('left', 'center', 'right'), loc=loc)
3495+
3496+
if loc == 'left':
3497+
kwargs.update(x=0, horizontalalignment='left')
3498+
elif loc == 'center':
3499+
kwargs.update(x=0.5, horizontalalignment='center')
3500+
elif loc == 'right':
3501+
kwargs.update(x=1, horizontalalignment='right')
3502+
34993503
return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
35003504

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

38453853
def invert_yaxis(self):

lib/matplotlib/tests/test_axes.py

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

107107

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

0 commit comments

Comments
 (0)