Skip to content

Commit

Permalink
Adds kwarg which to Axis.get_ticklabels, Axes.get_xticklabels,
Browse files Browse the repository at this point in the history
and Axes.get_yticklabels to match the pattern in other tick-related
functions

Closes matplotlib#1715
  • Loading branch information
tacaswell committed Nov 30, 2013
1 parent 350320f commit d57f138
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
12 changes: 10 additions & 2 deletions doc/users/whats_new.rst
Expand Up @@ -55,9 +55,9 @@ He also increased the performance for all of these functions and plot types.

Support for detrending and windowing 2D arrays in mlab
``````````````````````````````````````````````````````
Todd Jennings added support for 2D arrays in the
Todd Jennings added support for 2D arrays in the
:func:`~matplotlib.mlab.detrend_mean`, :func:`~matplotlib.mlab.detrend_none`,
and :func:`~matplotlib.mlab.detrend`, as well as adding
and :func:`~matplotlib.mlab.detrend`, as well as adding
:func:`~matplotlib.mlab.apply_window` which support windowing 2D arrays.

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

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

Date handling
-------------
Expand Down
46 changes: 40 additions & 6 deletions lib/matplotlib/axes/_base.py
Expand Up @@ -2601,13 +2601,30 @@ def get_xminorticklabels(self):
return cbook.silent_list('Text xticklabel',
self.xaxis.get_minorticklabels())

def get_xticklabels(self, minor=False):
def get_xticklabels(self, minor=False, which=None):
"""
Get the x tick labels as a list of :class:`~matplotlib.text.Text`
instances.
Parameter
---------
minor : bool
If True return the minor ticklabels,
else return the major ticklabels
which : None, ('minor', 'major', 'both')
Overrides `minor`.
Selects which ticklabels to return
Returns
-------
ret : list
List of :class:`~matplotlib.text.Text` instances.
"""
return cbook.silent_list('Text xticklabel',
self.xaxis.get_ticklabels(minor=minor))
self.xaxis.get_ticklabels(minor=minor,
which=which))

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

def get_yticklabels(self, minor=False):
def get_yticklabels(self, minor=False, which=None):
"""
Get the y tick labels as a list of :class:`~matplotlib.text.Text`
instances
Get the x tick labels as a list of :class:`~matplotlib.text.Text`
instances.
Parameter
---------
minor : bool
If True return the minor ticklabels,
else return the major ticklabels
which : None, ('minor', 'major', 'both')
Overrides `minor`.
Selects which ticklabels to return
Returns
-------
ret : list
List of :class:`~matplotlib.text.Text` instances.
"""
return cbook.silent_list('Text yticklabel',
self.yaxis.get_ticklabels(minor=minor))
self.yaxis.get_ticklabels(minor=minor,
which=which))

@docstring.dedent_interpd
def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):
Expand Down
34 changes: 32 additions & 2 deletions lib/matplotlib/axis.py
Expand Up @@ -1162,8 +1162,38 @@ def get_minorticklabels(self):
labels2 = [tick.label2 for tick in ticks if tick.label2On]
return cbook.silent_list('Text minor ticklabel', labels1 + labels2)

def get_ticklabels(self, minor=False):
'Return a list of Text instances for ticklabels'
def get_ticklabels(self, minor=False, which=None):
"""
Get the x tick labels as a list of :class:`~matplotlib.text.Text`
instances.
Parameter
---------
minor : bool
If True return the minor ticklabels,
else return the major ticklabels
which : None, ('minor', 'major', 'both')
Overrides `minor`.
Selects which ticklabels to return
Returns
-------
ret : list
List of :class:`~matplotlib.text.Text` instances.
"""

if which is not None:
if which == 'minor':
return self.get_minorticklabels()
elif which == 'major':
return self.get_majorticklabels()
elif which =='both':
return self.get_majorticklabels() + self.get_minorticklabels()
else:
raise ValueError("`which` must be one of ('minor', 'major', 'both')" +
"not " + str(which))
if minor:
return self.get_minorticklabels()
return self.get_majorticklabels()
Expand Down

0 comments on commit d57f138

Please sign in to comment.