8000 Backport PR #9670: Make tick_left/right keep labels off if they are a… · matplotlib/matplotlib@3698cca · GitHub
[go: up one dir, main page]

Skip to content

Commit 3698cca

Browse files
tacaswellMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #9670: Make tick_left/right keep labels off if they are already off
1 parent ba79e99 commit 3698cca

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

lib/matplotlib/axis.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,12 +1937,30 @@ def set_ticks_position(self, position):
19371937
self.stale = True
19381938

19391939
def tick_top(self):
1940-
'use ticks only on top'
1940+
"""
1941+
Move ticks and ticklabels (if present) to the top of the axes.
1942+
"""
1943+
label = True
1944+
if 'label1On' in self._major_tick_kw:
1945+
label = (self._major_tick_kw['label1On']
1946+
or self._major_tick_kw['label2On'])
19411947
self.set_ticks_position('top')
1948+
# if labels were turned off before this was called
1949+
# leave them off
1950+
self.set_tick_params(which='both', labeltop=label)
19421951

19431952
def tick_bottom(self):
1944-
'use ticks only on bottom'
1953+
"""
1954+
Move ticks and ticklabels (if present) to the bottom of the axes.
1955+
"""
1956+
label = True
1957+
if 'label1On' in self._major_tick_kw:
1958+
label = (self._major_tick_kw['label1On']
1959+
or self._major_tick_kw['label2On'])
19451960
self.set_ticks_position('bottom')
1961+
# if labels were turned off before this was called
1962+
# leave them off
1963+
self.set_tick_params(which='both', labelbottom=label)
19461964

19471965
def get_ticks_position(self):
19481966
"""
@@ -2273,12 +2291,30 @@ def set_ticks_position(self, position):
22732291
self.stale = True
22742292

22752293
def tick_right(self):
2276-
'use ticks only on right'
2294+
"""
2295+
Move ticks and ticklabels (if present) to the right of the axes.
2296+
"""
2297+
label = True
2298+
if 'label1On' in self._major_tick_kw:
2299+
label = (self._major_tick_kw['label1On']
2300+
or self._major_tick_kw['label2On'])
22772301
self.set_ticks_position('right')
2302+
# if labels were turned off before this was called
2303+
# leave them off
2304+
self.set_tick_params(which='both', labelright=label)
22782305

22792306
def tick_left(self):
2280-
'use ticks only on left'
2307+
"""
2308+
Move ticks and ticklabels (if present) to the left of the axes.
2309+
"""
2310+
label = True
2311+
if 'label1On' in self._major_tick_kw:
2312+
label = (self._major_tick_kw['label1On']
2313+
or self._major_tick_kw['label2On'])
22812314
self.set_ticks_position('left')
2315+
# if labels were turned off before this was called
2316+
# leave them off
2317+
self.set_tick_params(which='both', labelleft=label)
22822318

22832319
def get_ticks_position(self):
22842320
"""

lib/matplotlib/tests/test_subplots.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ def test_shared():
101101
check_visible(axs, [False, False, True, True], [True, False, True, False])
102102

103103

104+
def test_shared_and_moved():
105+
# test if sharey is on, but then tick_left is called that labels don't
106+
# re-appear. Seaborn does this just to be sure yaxis is on left...
107+
f, (a1, a2) = plt.subplots(1, 2, sharey=True)
108+
check_visible([a2], [True], [False])
109+
a2.yaxis.tick_left()
110+
check_visible([a2], [True], [False])
111+
112+
f, (a1, a2) = plt.subplots(2, 1, sharex=True)
113+
check_visible([a1], [False], [True])
114+
a2.xaxis.tick_bottom()
115+
check_visible([a1], [False], [True])
116+
117+
104118
def test_exceptions():
105119
# TODO should this test more options?
106120
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)
0