8000 Adds kwarg `which` to Axis.get_ticklabels, Axes.get_xticklabels, · matplotlib/matplotlib@08d961a · GitHub
[go: up one dir, main page]

Skip to content

Commit 08d961a

Browse files
committed
Adds kwarg which to Axis.get_ticklabels, Axes.get_xticklabels,
and Axes.get_yticklabels to match the pattern in other tick-related functions Closes #1715
1 parent 93cce5e commit 08d961a

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

doc/users/whats_new.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ Consistent grid sizes in streamplots
8080
`density=1` and `density=(1, 1)`. Previously a grid size of 30x30 was used for
8181
`density=1`, but a grid size of 25x25 was used for `density=(1, 1)`.
8282

83+
Get a list of all tick labels (major and minor)
84+
```````````````````````````````````````````````
85+
Added the `kwarg` 'which' to :func:`~matplotlib.Axes.get_xticklabels`,
86+
:func:`~matplotlib.Axes.get_yticklabels` and
87+
:func:`~matplotlib.Axis.get_ticklabels`. 'which' can be 'major', 'minor', or
88+
'both' select which ticks to return, like
89+
:func:`~matplotlib.Axis.set_ticks_position`. If 'which' is `None` then the old
90+
behaviour (controlled by the bool `minor`).
8391

8492
Date handling
8593
-------------

lib/matplotlib/axes/_base.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,13 +2601,30 @@ def get_xminorticklabels(self):
26012601
return cbook.silent_list('Text xticklabel',
26022602
self.xaxis.get_minorticklabels())
26032603

2604-
def get_xticklabels(self, minor=False):
2604+
def get_xticklabels(self, minor=False, which=None):
26052605
"""
26062606
Get the x tick labels as a list of :class:`~matplotlib.text.Text`
26072607
instances.
2608+
2609+
Parameter
2610+
---------
2611+
minor : bool
2612+
If True return the minor ticklabels,
2613+
else return the major ticklabels
2614+
2615+
which : None, ('minor', 'major', 'both')
2616+
Overrides `minor`.
2617+
2618+
Selects which ticklabels to return
2619+
2620+
Returns
2621+
-------
2622+
ret : list
2623+
List of :class:`~matplotlib.text.Text` instances.
26082624
"""
26092625
return cbook.silent_list('Text xticklabel',
2610-
self.xaxis.get_ticklabels(minor=minor))
2626+
self.xaxis.get_ticklabels(minor=minor,
2627+
which=which))
26112628

26122629
@docstring.dedent_interpd
26132630
def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs):
@@ -2837,13 +2854,30 @@ def get_yminorticklabels(self):
28372854
return cbook.silent_list('Text yticklabel',
28382855
self.yaxis.get_minorticklabels())
28392856

2840-
def get_yticklabels(self, minor=False):
2857+
def get_yticklabels(self, minor=False, which=None):
28412858
"""
2842-
Get the y tick labels as a list of :class:`~matplotlib.text.Text`
2843-
instances
2859+
Get the x tick labels as a list of :class:`~matplotlib.text.Text`
2860+
instances.
2861+
2862+
Parameter
2863+
---------
2864+
minor : bool
2865+
If True return the minor ticklabels,
2866+
else return the major ticklabels
2867+
2868+
which : None, ('minor', 'major', 'both')
2869+
Overrides `minor`.
2870+
2871+
Selects which ticklabels to return
2872+
2873+
Returns
2874+
-------
2875+
ret : list
2876+
List of :class:`~matplotlib.text.Text` instances.
28442877
"""
28452878
return cbook.silent_list('Text yticklabel',
2846-
self.yaxis. 8000 get_ticklabels(minor=minor))
2879+
self.yaxis.get_ticklabels(minor=minor,
2880+
which=which))
28472881

28482882
@docstring.dedent_interpd
28492883
def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):

lib/matplotlib/axis.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,38 @@ def get_minorticklabels(self):
11661166
labels2 = [tick.label2 for tick in ticks if tick.label2On]
11671167
return cbook.silent_list('Text minor ticklabel', labels1 + labels2)
11681168

1169-
def get_ticklabels(self, minor=False):
1170-
'Return a list of Text instances for ticklabels'
1169+
def get_ticklabels(self, minor=False, which=None):
1170+
"""
1171+
Get the x tick labels as a list of :class:`~matplotlib.text.Text`
1172+
instances.
1173+
1174+
Parameter
1175+
---------
1176+
minor : bool
1177+
If True return the minor ticklabels,
1178+
else return the major ticklabels
1179+
1180+
which : None, ('minor', 'major', 'both')
1181+
Overrides `minor`.
1182+
1183+
Selects which ticklabels to return
1184+
1185+
Returns
1186+
-------
1187+
ret : list
1188+
List of :class:`~matplotlib.text.Text` instances.
1189+
"""
1190+
1191+
if which is not None:
1192+
if which == 'minor':
1193+
return self.get_minorticklabels()
1194+
elif which == 'major':
1195+
return self.get_majorticklabels()
1196+
elif which =='both':
1197+
return self.get_majorticklabels() + self.get_minorticklabels()
1198+
else:
1199+
raise ValueError("`which` must be one of ('minor', 'major', 'both')" +
1200+
"not " + str(which))
11711201
if minor:
11721202
return self.get_minorticklabels()
11731203
return self.get_majorticklabels()

0 commit comments

Comments
 (0)
0