Skip to content

Commit d57f138

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 matplotlib#1715
1 parent 350320f commit d57f138

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

doc/users/whats_new.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ He also increased the performance for all of these functions and plot types.
5555

5656
Support for detrending and windowing 2D arrays in mlab
5757
``````````````````````````````````````````````````````
58-
Todd Jennings added support for 2D arrays in the
58+
Todd Jennings added support for 2D arrays in the
5959
:func:`~matplotlib.mlab.detrend_mean`, :func:`~matplotlib.mlab.detrend_none`,
60-
and :func:`~matplotlib.mlab.detrend`, as well as adding
60+
and :func:`~matplotlib.mlab.detrend`, as well as adding
6161
:func:`~matplotlib.mlab.apply_window` which support windowing 2D arrays.
6262

6363
Support for strides in mlab
@@ -68,6 +68,14 @@ strides to create memory-efficient 2D arrays. This includes
6868
array, and :func:`~matplotlib.mlab.stride_windows`, which uses a moving window
6969
to create a 2D array from a 1D array.
7070

71+
Get a list of all tick labels (major and minor)
72+
```````````````````````````````````````````````
73+
Added the `kwarg` 'which' to :func:`~matplotlib.Axes.get_xticklabels`,
74+
:func:`~matplotlib.Axes.get_yticklabels` and
75+
:func:`~matplotlib.Axis.get_ticklabels`. 'which' can be 'major', 'minor', or
76+
'both' select which ticks to return, like
77+
:func:`~matplotlib.Axis.set_ticks_position`. If 'which' is `None` then the old
78+
behaviour (controlled by the bool `minor`).
7179

7280
Date handling
7381
-------------

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.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
@@ -1162,8 +1162,38 @@ def get_minorticklabels(self):
11621162
labels2 = [tick.label2 for tick in ticks if tick.label2On]
11631163
return cbook.silent_list('Text minor ticklabel', labels1 + labels2)
11641164

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

0 commit comments

Comments
 (0)