From 47f9b6299e47c7da76d759968e3eaf0af5dd8582 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Sun, 28 Nov 2021 06:48:45 +0800 Subject: [PATCH 01/13] Fix label shift and add test --- lib/matplotlib/axes/_base.py | 8 ++++++-- lib/matplotlib/tests/test_axes.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 7a309780c3d7..f423530fd6e6 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3494,6 +3494,8 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, _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) @@ -3837,9 +3839,11 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) if loc == 'bottom': - kwargs.update(y=0, horizontalalignment='left') + kwargs.update(y=0, verticalalignment='bottom') + elif loc == 'center': + kwargs.update(y=0.5, verticalalignment='center') elif loc == 'top': - kwargs.update(y=1, horizontalalignment='right') + kwargs.update(y=1, verticalalignment='top') 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 5f6d528a3634..27d86d450e89 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -105,6 +105,30 @@ 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()._x == 0.5 + ax.set_xlabel("Test label", loc="right") + assert ax.xaxis.get_label()._x == 1.0 + ax.set_xlabel("Test label", loc="center") + assert ax.xaxis.get_label()._x == 0.5 + + # 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()._y == 0.5 + ax.set_ylabel("Test label", loc="bottom") + assert ax.yaxis.get_label()._y == 0.0 + ax.set_ylabel("Test label", loc="center") + assert ax.yaxis.get_label()._y == 0.5 + + plt.close() + + @check_figures_equal(extensions=["png"]) def test_acorr(fig_test, fig_ref): np.random.seed(19680801) From 261c55947f31deee19048bf91ccb8a0f288a661b Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Sun, 28 Nov 2021 18:01:13 +0800 Subject: [PATCH 02/13] Use public api and revert changes --- lib/matplotlib/axes/_base.py | 6 +++--- lib/matplotlib/tests/test_axes.py | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index f423530fd6e6..e25a5520ca06 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3839,11 +3839,11 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) if loc == 'bottom': - kwargs.update(y=0, verticalalignment='bottom') + kwargs.update(y=0, horizontalalignment='left') elif loc == 'center': - kwargs.update(y=0.5, verticalalignment='center') + kwargs.update(y=0.5, horizontalalignment='center') elif loc == 'top': - kwargs.update(y=1, verticalalignment='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 27d86d450e89..035424bf279a 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -111,20 +111,20 @@ def test_label_shift(): # 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()._x == 0.5 + assert ax.xaxis.get_label().get_horizontalalignment() == "center" ax.set_xlabel("Test label", loc="right") - assert ax.xaxis.get_label()._x == 1.0 + assert ax.xaxis.get_label().get_horizontalalignment() == "right" ax.set_xlabel("Test label", loc="center") - assert ax.xaxis.get_label()._x == 0.5 + 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()._y == 0.5 + assert ax.yaxis.get_label().get_horizontalalignment() == "center" ax.set_ylabel("Test label", loc="bottom") - assert ax.yaxis.get_label()._y == 0.0 + assert ax.yaxis.get_label().get_horizontalalignment() == "left" ax.set_ylabel("Test label", loc="center") - assert ax.yaxis.get_label()._y == 0.5 + assert ax.yaxis.get_label().get_horizontalalignment() == "center" plt.close() From 07dfc1e29bdb0343d3cf7955c848ca2120879542 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Mon, 29 Nov 2021 00:31:12 +0800 Subject: [PATCH 03/13] Resolve ha conflict --- lib/matplotlib/axes/_base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index e25a5520ca06..ba7560a576ad 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3492,6 +3492,7 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['xaxis.labellocation']) _api.check_in_list(('left', 'center', 'right'), loc=loc) + kwargs.pop('ha', None) if loc == 'left': kwargs.update(x=0, horizontalalignment='left') elif loc == 'center': @@ -3838,6 +3839,7 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) + kwargs.pop('ha', None) if loc == 'bottom': kwargs.update(y=0, horizontalalignment='left') elif loc == 'center': From d93cfb51836510db31d1cb10729935567e0d4360 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Mon, 29 Nov 2021 02:27:44 +0800 Subject: [PATCH 04/13] Allow ha overwrite --- lib/matplotlib/axes/_base.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index ba7560a576ad..48f933073126 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3492,13 +3492,14 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['xaxis.labellocation']) _api.check_in_list(('left', 'center', 'right'), loc=loc) + horizontalalignment = None or kwargs.get('horizontalalignment') or kwargs.get('ha') kwargs.pop('ha', None) if loc == 'left': - kwargs.update(x=0, horizontalalignment='left') + kwargs.update(x=0, horizontalalignment=horizontalalignment or 'left') elif loc == 'center': - kwargs.update(x=0.5, horizontalalignment='center') + kwargs.update(x=0.5, horizontalalignment=horizontalalignment or 'center') elif loc == 'right': - kwargs.update(x=1, horizontalalignment='right') + kwargs.update(x=1, horizontalalignment=horizontalalignment or 'right') return self.xaxis.set_label_text(xlabel, fontdict, **kwargs) def invert_xaxis(self): @@ -3839,13 +3840,14 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) + horizontalalignment = None or kwargs.get('horizontalalignment') or kwargs.get('ha') kwargs.pop('ha', None) if loc == 'bottom': - kwargs.update(y=0, horizontalalignment='left') + kwargs.update(y=0, horizontalalignment=horizontalalignment or 'left') elif loc == 'center': - kwargs.update(y=0.5, horizontalalignment='center') + kwargs.update(y=0.5, horizontalalignment=horizontalalignment or 'center') elif loc == 'top': - kwargs.update(y=1, horizontalalignment='right') + kwargs.update(y=1, horizontalalignment=horizontalalignment or 'right') return self.yaxis.set_label_text(ylabel, fontdict, **kwargs) def invert_yaxis(self): From f2672c92106bbb94bb4d8f24361d357cd7f3ae4c Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Mon, 29 Nov 2021 02:32:45 +0800 Subject: [PATCH 05/13] Allow ha overwrite --- lib/matplotlib/axes/_base.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 48f933073126..fde9d05a3ebc 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3492,14 +3492,14 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['xaxis.labellocation']) _api.check_in_list(('left', 'center', 'right'), loc=loc) - horizontalalignment = None or kwargs.get('horizontalalignment') or kwargs.get('ha') - kwargs.pop('ha', None) + ha = None or kwargs.get('horizontalalignment') or kwargs.get('ha') + kwargs.pop('horizontalalignment', None) if loc == 'left': - kwargs.update(x=0, horizontalalignment=horizontalalignment or 'left') + kwargs.update(x=0, ha=ha or 'left') elif loc == 'center': - kwargs.update(x=0.5, horizontalalignment=horizontalalignment or 'center') + kwargs.update(x=0.5, ha=ha or 'center') elif loc == 'right': - kwargs.update(x=1, horizontalalignment=horizontalalignment or 'right') + kwargs.update(x=1, ha=ha or 'right') return self.xaxis.set_label_text(xlabel, fontdict, **kwargs) def invert_xaxis(self): @@ -3840,14 +3840,14 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) - horizontalalignment = None or kwargs.get('horizontalalignment') or kwargs.get('ha') - kwargs.pop('ha', None) + ha = None or kwargs.get('horizontalalignment') or kwargs.get('ha') + kwargs.pop('horizontalalignment', None) if loc == 'bottom': - kwargs.update(y=0, horizontalalignment=horizontalalignment or 'left') + kwargs.update(y=0, ha=ha or 'left') elif loc == 'center': - kwargs.update(y=0.5, horizontalalignment=horizontalalignment or 'center') + kwargs.update(y=0.5, ha=ha or 'center') elif loc == 'top': - kwargs.update(y=1, horizontalalignment=horizontalalignment or 'right') + kwargs.update(y=1, ha=ha or 'right') return self.yaxis.set_label_text(ylabel, fontdict, **kwargs) def invert_yaxis(self): From 2d382bd9acb973e262c738999a23e372977c56a7 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Mon, 29 Nov 2021 03:03:15 +0800 Subject: [PATCH 06/13] Allox x/y overwrite --- lib/matplotlib/axes/_base.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index fde9d05a3ebc..abe4b5da92ee 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3493,13 +3493,14 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, else mpl.rcParams['xaxis.labellocation']) _api.check_in_list(('left', 'center', 'right'), loc=loc) ha = None or kwargs.get('horizontalalignment') or kwargs.get('ha') + x = None or kwargs.get('x') kwargs.pop('horizontalalignment', None) if loc == 'left': - kwargs.update(x=0, ha=ha or 'left') + kwargs.update(x=x or 0, ha=ha or 'left') elif loc == 'center': - kwargs.update(x=0.5, ha=ha or 'center') + kwargs.update(x=x or 0.5, ha=ha or 'center') elif loc == 'right': - kwargs.update(x=1, ha=ha or 'right') + kwargs.update(x=x or 1, ha=ha or 'right') return self.xaxis.set_label_text(xlabel, fontdict, **kwargs) def invert_xaxis(self): @@ -3841,13 +3842,14 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) ha = None or kwargs.get('horizontalalignment') or kwargs.get('ha') + y = None or kwargs.get('y') kwargs.pop('horizontalalignment', None) if loc == 'bottom': - kwargs.update(y=0, ha=ha or 'left') + kwargs.update(y=y or 0, ha=ha or 'left') elif loc == 'center': - kwargs.update(y=0.5, ha=ha or 'center') + kwargs.update(y=y or 0.5, ha=ha or 'center') elif loc == 'top': - kwargs.update(y=1, ha=ha or 'right') + kwargs.update(y=y or 1, ha=ha or 'right') return self.yaxis.set_label_text(ylabel, fontdict, **kwargs) def invert_yaxis(self): From 76a4672479e03068d858b67b0f7cc2a5562817aa Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Mon, 29 Nov 2021 03:51:07 +0800 Subject: [PATCH 07/13] Fix bug --- lib/matplotlib/axes/_base.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index abe4b5da92ee..d8f4af1e936b 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3495,12 +3495,19 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, ha = None or kwargs.get('horizontalalignment') or kwargs.get('ha') x = None or kwargs.get('x') kwargs.pop('horizontalalignment', None) + if loc == 'left': - kwargs.update(x=x or 0, ha=ha or 'left') + kwargs.update(x=0 if x is None else x, + ha='left' if ha is None else ha) + elif loc == 'center': - kwargs.update(x=x or 0.5, ha=ha or 'center') + kwargs.update(x=0.5 if x is None else x, + ha='center' if ha is None else ha) + elif loc == 'right': - kwargs.update(x=x or 1, ha=ha or 'right') + kwargs.update(x=1 if x is None else x, + ha='right' if ha is None else ha) + return self.xaxis.set_label_text(xlabel, fontdict, **kwargs) def invert_xaxis(self): @@ -3844,12 +3851,19 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, ha = None or kwargs.get('horizontalalignment') or kwargs.get('ha') y = None or kwargs.get('y') kwargs.pop('horizontalalignment', None) + if loc == 'bottom': - kwargs.update(y=y or 0, ha=ha or 'left') + kwargs.update(y=0 if y is None else y, + ha='left' if ha is None else ha) + elif loc == 'center': - kwargs.update(y=y or 0.5, ha=ha or 'center') + kwargs.update(y=0.5 if y is None else y, + ha='center' if ha is None else ha) + elif loc == 'top': - kwargs.update(y=y or 1, ha=ha or 'right') + kwargs.update(y=1 if y is None else y, + ha='right' if ha is None else ha) + return self.yaxis.set_label_text(ylabel, fontdict, **kwargs) def invert_yaxis(self): From 83a3fb10eb812aeae24796927149bb553bf58188 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Mon, 29 Nov 2021 06:30:58 +0800 Subject: [PATCH 08/13] Throw aliasing error --- lib/matplotlib/axes/_base.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index d8f4af1e936b..05204956a2fd 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3492,21 +3492,26 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['xaxis.labellocation']) _api.check_in_list(('left', 'center', 'right'), loc=loc) - ha = None or kwargs.get('horizontalalignment') or kwargs.get('ha') x = None or kwargs.get('x') - kwargs.pop('horizontalalignment', None) + ha = None or kwargs.get('ha') + horizontalalignment = None or kwargs.get('horizontalalignment') + ha_to_use = horizontalalignment or ha + + # Use ha_to_use under the ha alias moving forward + if not (ha and horizontalalignment): + kwargs.pop('horizontalalignment', None) if loc == 'left': kwargs.update(x=0 if x is None else x, - ha='left' if ha is None else ha) + ha='left' if ha_to_use is None else ha_to_use) elif loc == 'center': kwargs.update(x=0.5 if x is None else x, - ha='center' if ha is None else ha) + ha='center' if ha_to_use is None else ha_to_use) elif loc == 'right': kwargs.update(x=1 if x is None else x, - ha='right' if ha is None else ha) + ha='right' if ha_to_use is None else ha_to_use) return self.xaxis.set_label_text(xlabel, fontdict, **kwargs) @@ -3848,21 +3853,26 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) - ha = None or kwargs.get('horizontalalignment') or kwargs.get('ha') y = None or kwargs.get('y') - kwargs.pop('horizontalalignment', None) + ha = None or kwargs.get('ha') + horizontalalignment = None or kwargs.get('horizontalalignment') + ha_to_use = horizontalalignment or ha + + # Use ha_to_use under the ha alias moving forward + if not (ha and horizontalalignment): + kwargs.pop('horizontalalignment', None) if loc == 'bottom': kwargs.update(y=0 if y is None else y, - ha='left' if ha is None else ha) + ha='left' if ha_to_use is None else ha_to_use) elif loc == 'center': kwargs.update(y=0.5 if y is None else y, - ha='center' if ha is None else ha) + ha='center' if ha_to_use is None else ha_to_use) elif loc == 'top': kwargs.update(y=1 if y is None else y, - ha='right' if ha is None else ha) + ha='right' if ha_to_use is None else ha_to_use) return self.yaxis.set_label_text(ylabel, fontdict, **kwargs) From 779bd581aa45ccabc0f9a13dc2162e5f271f9c4e Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Tue, 30 Nov 2021 08:21:44 +0800 Subject: [PATCH 09/13] Remove plt.close() --- lib/matplotlib/tests/test_axes.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 035424bf279a..5da2df1455db 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -126,8 +126,6 @@ def test_label_shift(): ax.set_ylabel("Test label", loc="center") assert ax.yaxis.get_label().get_horizontalalignment() == "center" - plt.close() - @check_figures_equal(extensions=["png"]) def test_acorr(fig_test, fig_ref): From bf9e4bdca77199cfa017ca64a20e166843c1b569 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Tue, 30 Nov 2021 08:30:34 +0800 Subject: [PATCH 10/13] Remove redundancy --- lib/matplotlib/axes/_base.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 05204956a2fd..54e56b11696d 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3492,9 +3492,9 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['xaxis.labellocation']) _api.check_in_list(('left', 'center', 'right'), loc=loc) - x = None or kwargs.get('x') - ha = None or kwargs.get('ha') - horizontalalignment = None or kwargs.get('horizontalalignment') + x = kwargs.get('x') + ha = kwargs.get('ha') + horizontalalignment = kwargs.get('horizontalalignment') ha_to_use = horizontalalignment or ha # Use ha_to_use under the ha alias moving forward @@ -3853,9 +3853,9 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) - y = None or kwargs.get('y') - ha = None or kwargs.get('ha') - horizontalalignment = None or kwargs.get('horizontalalignment') + y = kwargs.get('y') + ha = kwargs.get('ha') + horizontalalignment = kwargs.get('horizontalalignment') ha_to_use = horizontalalignment or ha # Use ha_to_use under the ha alias moving forward From 13495fce4cb0ef0d847fc4ead4ba1488d72d0bdc Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Wed, 1 Dec 2021 08:44:26 +0800 Subject: [PATCH 11/13] Call normalize_kwargs() --- lib/matplotlib/axes/_base.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 54e56b11696d..f302267f246f 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3492,26 +3492,21 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['xaxis.labellocation']) _api.check_in_list(('left', 'center', 'right'), loc=loc) + kwargs = cbook.normalize_kwargs(kwargs, mtext.Text) x = kwargs.get('x') - ha = kwargs.get('ha') - horizontalalignment = kwargs.get('horizontalalignment') - ha_to_use = horizontalalignment or ha - - # Use ha_to_use under the ha alias moving forward - if not (ha and horizontalalignment): - kwargs.pop('horizontalalignment', None) + ha = kwargs.get('horizontalalignment') if loc == 'left': kwargs.update(x=0 if x is None else x, - ha='left' if ha_to_use is None else ha_to_use) + horizontalalignment='left' if ha is None else ha) elif loc == 'center': kwargs.update(x=0.5 if x is None else x, - ha='center' if ha_to_use is None else ha_to_use) + horizontalalignment='center' if ha is None else ha) elif loc == 'right': kwargs.update(x=1 if x is None else x, - ha='right' if ha_to_use is None else ha_to_use) + horizontalalignment='right' if ha is None else ha) return self.xaxis.set_label_text(xlabel, fontdict, **kwargs) @@ -3853,26 +3848,21 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, loc = (loc if loc is not None else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) + kwargs = cbook.normalize_kwargs(kwargs, mtext.Text) y = kwargs.get('y') - ha = kwargs.get('ha') - horizontalalignment = kwargs.get('horizontalalignment') - ha_to_use = horizontalalignment or ha - - # Use ha_to_use under the ha alias moving forward - if not (ha and horizontalalignment): - kwargs.pop('horizontalalignment', None) + ha = kwargs.get('horizontalalignment') if loc == 'bottom': kwargs.update(y=0 if y is None else y, - ha='left' if ha_to_use is None else ha_to_use) + horizontalalignment='left' if ha is None else ha) elif loc == 'center': kwargs.update(y=0.5 if y is None else y, - ha='center' if ha_to_use is None else ha_to_use) + horizontalalignment='center' if ha is None else ha) elif loc == 'top': kwargs.update(y=1 if y is None else y, - ha='right' if ha_to_use is None else ha_to_use) + horizontalalignment='right' if ha is None else ha) return self.yaxis.set_label_text(ylabel, fontdict, **kwargs) From 59a936da0f0918d8973d14b26734a6820af64350 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Wed, 1 Dec 2021 23:33:36 +0800 Subject: [PATCH 12/13] Use setdefault() --- lib/matplotlib/axes/_base.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index f302267f246f..b481db1c2342 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3493,20 +3493,18 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *, else mpl.rcParams['xaxis.labellocation']) _api.check_in_list(('left', 'center', 'right'), loc=loc) kwargs = cbook.normalize_kwargs(kwargs, mtext.Text) - x = kwargs.get('x') - ha = kwargs.get('horizontalalignment') if loc == 'left': - kwargs.update(x=0 if x is None else x, - horizontalalignment='left' if ha is None else ha) + kwargs.setdefault('x', 0) + kwargs.setdefault('horizontalalignment', 'left') elif loc == 'center': - kwargs.update(x=0.5 if x is None else x, - horizontalalignment='center' if ha is None else ha) + kwargs.setdefault('x', 0.5) + kwargs.setdefault('horizontalalignment', 'center') elif loc == 'right': - kwargs.update(x=1 if x is None else x, - horizontalalignment='right' if ha is None else ha) + kwargs.setdefault('x', 1) + kwargs.setdefault('horizontalalignment', 'right') return self.xaxis.set_label_text(xlabel, fontdict, **kwargs) @@ -3849,20 +3847,18 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, else mpl.rcParams['yaxis.labellocation']) _api.check_in_list(('bottom', 'center', 'top'), loc=loc) kwargs = cbook.normalize_kwargs(kwargs, mtext.Text) - y = kwargs.get('y') - ha = kwargs.get('horizontalalignment') if loc == 'bottom': - kwargs.update(y=0 if y is None else y, - horizontalalignment='left' if ha is None else ha) + kwargs.setdefault('y', 0) + kwargs.setdefault('horizontalalignment', 'left') elif loc == 'center': - kwargs.update(y=0.5 if y is None else y, - horizontalalignment='center' if ha is None else ha) + kwargs.setdefault('y', 0.5) + kwargs.setdefault('horizontalalignment', 'center') elif loc == 'top': - kwargs.update(y=1 if y is None else y, - horizontalalignment='right' if ha is None else ha) + kwargs.setdefault('y', 1) + kwargs.setdefault('horizontalalignment', 'right') return self.yaxis.set_label_text(ylabel, fontdict, **kwargs) From b836a43e7656ea42ffdc1fd916a14d155c02ab26 Mon Sep 17 00:00:00 2001 From: Hansin Ahuja Date: Thu, 2 Dec 2021 14:08:29 +0800 Subject: [PATCH 13/13] Use loc absent x, ha --- lib/matplotlib/axes/_base.py | 46 +++++++++++++----------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index b481db1c2342..21d6f5ac552b 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3487,24 +3487,18 @@ 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) - kwargs = cbook.normalize_kwargs(kwargs, mtext.Text) - - if loc == 'left': - kwargs.setdefault('x', 0) - kwargs.setdefault('horizontalalignment', 'left') + _api.check_in_list(('left', 'center', 'right'), loc=loc) - elif loc == 'center': - kwargs.setdefault('x', 0.5) - kwargs.setdefault('horizontalalignment', 'center') - - elif loc == 'right': - kwargs.setdefault('x', 1) - kwargs.setdefault('horizontalalignment', 'right') + 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) @@ -3841,24 +3835,18 @@ 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) - kwargs = cbook.normalize_kwargs(kwargs, mtext.Text) - - if loc == 'bottom': - kwargs.setdefault('y', 0) - kwargs.setdefault('horizontalalignment', 'left') - - elif loc == 'center': - kwargs.setdefault('y', 0.5) - kwargs.setdefault('horizontalalignment', 'center') - - elif loc == 'top': - kwargs.setdefault('y', 1) - kwargs.setdefault('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) 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