From 76893487ca7905e913424746975a878df077930f Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 2 Nov 2017 13:06:27 -0700 Subject: [PATCH 1/5] Make tick_left/right keep labels off if they are already off --- lib/matplotlib/axis.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 3c00d5e7176a..3f5beba39045 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -2275,11 +2275,22 @@ def set_ticks_position(self, position): def tick_right(self): 'use ticks only on right' + label = (self._major_tick_kw['label1On'] + or self._major_tick_kw['label2On']) self.set_ticks_position('right') + # if labels were turned off before this was called + # leave them off + self.set_tick_params(which='both', labelright=label) def tick_left(self): 'use ticks only on left' + label = (self._major_tick_kw['label1On'] + or self._major_tick_kw['label2On']) self.set_ticks_position('left') + # if labels were turned off before this was called + # leave them off + self.set_tick_params(which='both', labelleft=label) + def get_ticks_position(self): """ From 555171fc31bf44f6f283f5bb00f092afb4b5c412 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 2 Nov 2017 13:35:56 -0700 Subject: [PATCH 2/5] Make tick_left/right keep labels off if they are already off --- lib/matplotlib/axis.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 3f5beba39045..adefdca7c006 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -2275,8 +2275,10 @@ def set_ticks_position(self, position): def tick_right(self): 'use ticks only on right' - label = (self._major_tick_kw['label1On'] - or self._major_tick_kw['label2On']) + label = True + if 'label1On' in self._major_tick_kw: + label = (self._major_tick_kw['label1On'] + or self._major_tick_kw['label2On']) self.set_ticks_position('right') # if labels were turned off before this was called # leave them off @@ -2284,8 +2286,10 @@ def tick_right(self): def tick_left(self): 'use ticks only on left' - label = (self._major_tick_kw['label1On'] - or self._major_tick_kw['label2On']) + label = True + if 'label1On' in self._major_tick_kw: + label = (self._major_tick_kw['label1On'] + or self._major_tick_kw['label2On']) self.set_ticks_position('left') # if labels were turned off before this was called # leave them off From 8dac00d757ac85d69c84b7bb3c6218e01223b09a Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 2 Nov 2017 14:19:31 -0700 Subject: [PATCH 3/5] PEP8 --- lib/matplotlib/axis.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index adefdca7c006..eb1da2c60dcb 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -2295,7 +2295,6 @@ def tick_left(self): # leave them off self.set_tick_params(which='both', labelleft=label) - def get_ticks_position(self): """ Return the ticks position (left, right, both or unknown) From 20200a5031cd8ddd6495b96a24d8515f303e0e48 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 2 Nov 2017 14:21:05 -0700 Subject: [PATCH 4/5] tick_bottom/top --- lib/matplotlib/axis.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index eb1da2c60dcb..97d5f31c335e 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1939,11 +1939,25 @@ def set_ticks_position(self, position): def tick_top(self): 'use ticks only on top' + label = True + if 'label1On' in self._major_tick_kw: + label = (self._major_tick_kw['label1On'] + or self._major_tick_kw['label2On']) self.set_ticks_position('top') + # if labels were turned off before this was called + # leave them off + self.set_tick_params(which='both', labeltop=label) def tick_bottom(self): 'use ticks only on bottom' + label = True + if 'label1On' in self._major_tick_kw: + label = (self._major_tick_kw['label1On'] + or self._major_tick_kw['label2On']) self.set_ticks_position('bottom') + # if labels were turned off before this was called + # leave them off + self.set_tick_params(which='both', labelbottom=label) def get_ticks_position(self): """ From 17a615ec45ef44f68a11e5a32fbd57c0c9cc5ff4 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 3 Nov 2017 12:35:36 -0700 Subject: [PATCH 5/5] Added test --- lib/matplotlib/axis.py | 16 ++++++++++++---- lib/matplotlib/tests/test_subplots.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 97d5f31c335e..dae28e6d94cd 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1938,7 +1938,9 @@ def set_ticks_position(self, position): self.stale = True def tick_top(self): - 'use ticks only on top' + """ + Move ticks and ticklabels (if present) to the top of the axes. + """ label = True if 'label1On' in self._major_tick_kw: label = (self._major_tick_kw['label1On'] @@ -1949,7 +1951,9 @@ def tick_top(self): self.set_tick_params(which='both', labeltop=label) def tick_bottom(self): - 'use ticks only on bottom' + """ + Move ticks and ticklabels (if present) to the bottom of the axes. + """ label = True if 'label1On' in self._major_tick_kw: label = (self._major_tick_kw['label1On'] @@ -2288,7 +2292,9 @@ def set_ticks_position(self, position): self.stale = True def tick_right(self): - 'use ticks only on right' + """ + Move ticks and ticklabels (if present) to the right of the axes. + """ label = True if 'label1On' in self._major_tick_kw: label = (self._major_tick_kw['label1On'] @@ -2299,7 +2305,9 @@ def tick_right(self): self.set_tick_params(which='both', labelright=label) def tick_left(self): - 'use ticks only on left' + """ + Move ticks and ticklabels (if present) to the left of the axes. + """ label = True if 'label1On' in self._major_tick_kw: label = (self._major_tick_kw['label1On'] diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index b6a464051ff2..3546d8d66c7c 100644 --- a/lib/matplotlib/tests/test_subplots.py +++ b/lib/matplotlib/tests/test_subplots.py @@ -101,6 +101,20 @@ def test_shared(): check_visible(axs, [False, False, True, True], [True, False, True, False]) +def test_shared_and_moved(): + # test if sharey is on, but then tick_left is called that labels don't + # re-appear. Seaborn does this just to be sure yaxis is on left... + f, (a1, a2) = plt.subplots(1, 2, sharey=True) + check_visible([a2], [True], [False]) + a2.yaxis.tick_left() + check_visible([a2], [True], [False]) + + f, (a1, a2) = plt.subplots(2, 1, sharex=True) + check_visible([a1], [False], [True]) + a2.xaxis.tick_bottom() + check_visible([a1], [False], [True]) + + def test_exceptions(): # TODO should this test more options? with pytest.raises(ValueError):