Skip to content

FIX: Reset label of axis to center #21773

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 13 commits into from
Dec 10, 2021
32 changes: 20 additions & 12 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3487,15 +3487,19 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *,
f"its corresponding low level keyword "
f"arguments ({protected_kw}) are also "
f"supplied")
loc = 'center'

else:
loc = (loc if loc is not None
else mpl.rcParams['xaxis.labellocation'])
_api.check_in_list(('left', 'center', 'right'), loc=loc)
if loc == 'left':
kwargs.update(x=0, horizontalalignment='left')
elif loc == 'right':
kwargs.update(x=1, horizontalalignment='right')
_api.check_in_list(('left', 'center', 'right'), loc=loc)

if loc == 'left':
kwargs.update(x=0, horizontalalignment='left')
elif loc == 'center':
kwargs.update(x=0.5, horizontalalignment='center')
elif loc == 'right':
kwargs.update(x=1, horizontalalignment='right')

return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)

def invert_xaxis(self):
Expand Down Expand Up @@ -3831,15 +3835,19 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *,
f"its corresponding low level keyword "
f"arguments ({protected_kw}) are also "
f"supplied")
loc = 'center'

else:
loc = (loc if loc is not None
else mpl.rcParams['yaxis.labellocation'])
_api.check_in_list(('bottom', 'center', 'top'), loc=loc)
if loc == 'bottom':
kwargs.update(y=0, horizontalalignment='left')
elif loc == 'top':
kwargs.update(y=1, horizontalalignment='right')
_api.check_in_list(('bottom', 'center', 'top'), loc=loc)

if loc == 'bottom':
kwargs.update(y=0, horizontalalignment='left')
elif loc == 'center':
kwargs.update(y=0.5, horizontalalignment='center')
elif loc == 'top':
kwargs.update(y=1, horizontalalignment='right')

return self.yaxis.set_label_text(ylabel, fontdict, **kwargs)

def invert_yaxis(self):
Expand Down
22 changes: 22 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ def test_label_loc_rc(fig_test, fig_ref):
cbar.set_label("Z Label", x=1, ha='right')


def test_label_shift():
fig, ax = plt.subplots()

# Test label re-centering on x-axis
ax.set_xlabel("Test label", loc="left")
ax.set_xlabel("Test label", loc="center")
assert ax.xaxis.get_label().get_horizontalalignment() == "center"
ax.set_xlabel("Test label", loc="right")
assert ax.xaxis.get_label().get_horizontalalignment() == "right"
ax.set_xlabel("Test label", loc="center")
assert ax.xaxis.get_label().get_horizontalalignment() == "center"

# Test label re-centering on y-axis
ax.set_ylabel("Test label", loc="top")
ax.set_ylabel("Test label", loc="center")
assert ax.yaxis.get_label().get_horizontalalignment() == "center"
ax.set_ylabel("Test label", loc="bottom")
assert ax.yaxis.get_label().get_horizontalalignment() == "left"
ax.set_ylabel("Test label", loc="center")
assert ax.yaxis.get_label().get_horizontalalignment() == "center"


@check_figures_equal(extensions=["png"])
def test_acorr(fig_test, fig_ref):
np.random.seed(19680801)
Expand Down
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