diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 3c00d5e7176a..dae28e6d94cd 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1938,12 +1938,30 @@ 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'] + 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' + """ + 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'] + 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): """ @@ -2274,12 +2292,30 @@ 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'] + 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' + """ + 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'] + 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): """ 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):