From 108448319ba044efaeb9eceb60cda611dbb629a9 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja <48261100+hansinahuja@users.noreply.github.com> Date: Fri, 10 Dec 2021 10:57:26 +0530 Subject: [PATCH] Backport PR #21773: FIX: Reset label of axis to center --- lib/matplotlib/axes/_base.py | 32 +++++++++++++++++++------------ lib/matplotlib/tests/test_axes.py | 22 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 7cea32d849d0..77b81b5ac406 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3488,15 +3488,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): @@ -3832,15 +3836,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): diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 17f8e5c6312c..39b247863786 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -104,6 +104,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)
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: