Skip to content

Commit 9e5b1d6

Browse files
committed
Add labels parameter to set_ticks()
1 parent 153b463 commit 9e5b1d6

File tree

4 files changed

+85
-36
lines changed

4 files changed

+85
-36
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Settings tick positions and labels simultaneously in ``set_ticks``
2+
------------------------------------------------------------------
3+
`.Axis.set_ticks` (and the corresponding `.Axes.set_xticks` /
4+
`.Axes.set_yticks`) got a new parameter *labels* allowing to set tick positions
5+
and labels simultaneously.
6+
7+
Previously, setting tick labels was done using `.Axis.set_ticklabels` (or
8+
the corresponding `.Axes.set_xticklabels` / `.Axes.set_yticklabels`). This
9+
usually only makes sense if you previously fix the position with
10+
`~.Axis.set_ticks`. Both functionality is now available in `~.Axis.set_ticks`.
11+
The use of `.Axis.set_ticklabels` is discouraged, but it will stay available
12+
for backward compatibility.
13+
14+
Note: This addition makes the API of `~.Axis.set_ticks` also more similar to
15+
`.pyplot.xticks` / `.pyplot.yticks`, which already had the additional *labels*
16+
parameter.

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import matplotlib.docstring as docstring
55
import matplotlib.ticker as mticker
66
from matplotlib.axes._base import _AxesBase, _TransformedBoundsLocator
7+
from matplotlib.axis import Axis
78

89

910
class SecondaryAxis(_AxesBase):
@@ -123,18 +124,9 @@ def apply_aspect(self, position=None):
123124
self._set_lims()
124125
super().apply_aspect(position)
125126

126-
def set_ticks(self, ticks, *, minor=False):
127-
"""
128-
Set the x ticks with list of *ticks*
129-
130-
Parameters
131-
----------
132-
ticks : list
133-
List of x-axis tick locations.
134-
minor : bool, default: False
135-
If ``False`` sets major ticks, if ``True`` sets minor ticks.
136-
"""
137-
ret = self._axis.set_ticks(ticks, minor=minor)
127+
@docstring.copy(Axis.set_ticks)
128+
def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
129+
ret = self._axis.set_ticks(ticks, labels, minor=minor, **kwargs)
138130
self.stale = True
139131
self._ticks_set = True
140132
return ret

lib/matplotlib/axis.py

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,10 +1661,16 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
16611661
r"""
16621662
Set the text values of the tick labels.
16631663
1664-
.. warning::
1665-
This method should only be used after fixing the tick positions
1666-
using `.Axis.set_ticks`. Otherwise, the labels may end up in
1667-
unexpected positions.
1664+
.. admonition:: Discouraged
1665+
1666+
The use of this method is discouraged, because of the dependency
1667+
on tick positions. In most cases, you'll want to use
1668+
``set_[x/y]ticks(positions, labels)`` instead.
1669+
1670+
If you are using this method, you should always fix the tick
1671+
positions before, e.g. by using `.Axis.set_ticks` or by explicitly
1672+
setting a `~.ticker.FixedLocator`. Otherwise, ticks are free to
1673+
move and the labels may end up in unexpected positions.
16681674
16691675
Parameters
16701676
----------
@@ -1772,27 +1778,9 @@ def _set_ticklabels(self, labels, fontdict=None, minor=False, **kwargs):
17721778
kwargs.update(fontdict)
17731779
return self.set_ticklabels(labels, minor=minor, **kwargs)
17741780

1775-
def set_ticks(self, ticks, *, minor=False):
1776-
"""
1777-
Set this Axis' tick locations.
1778-
1779-
If necessary, the view limits of the Axis are expanded so that all
1780-
given ticks are visible.
1781+
def _set_tick_locations(self, ticks, *, minor=False):
1782+
# see docstring of set_ticks
17811783

1782-
Parameters
1783-
----------
1784-
ticks : list of floats
1785-
List of tick locations.
1786-
minor : bool, default: False
1787-
If ``False``, set the major ticks; if ``True``, the minor ticks.
1788-
1789-
Notes
1790-
-----
1791-
The mandatory expansion of the view limits is an intentional design
1792-
choice to prevent the surprise of a non-visible tick. If you need
1793-
other limits, you should set the limits explicitly after setting the
1794-
ticks.
1795-
"""
17961784
# XXX if the user changes units, the information will be lost here
17971785
ticks = self.convert_units(ticks)
17981786
if self is self.axes.xaxis:
@@ -1827,6 +1815,37 @@ def set_ticks(self, ticks, *, minor=False):
18271815
self.set_major_locator(mticker.FixedLocator(ticks))
18281816
return self.get_major_ticks(len(ticks))
18291817

1818+
def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
1819+
"""
1820+
Set this Axis' tick locations and optionally labels.
1821+
1822+
If necessary, the view limits of the Axis are expanded so that all
1823+
given ticks are visible.
1824+
1825+
Parameters
1826+
----------
1827+
ticks : list of floats
1828+
List of tick locations.
1829+
labels : list of str, optional
1830+
List of tick labels. If not set, the labels show the data value.
1831+
minor : bool, default: False
1832+
If ``False``, set the major ticks; if ``True``, the minor ticks.
1833+
**kwargs
1834+
`.Text` properties for the labels. These take effect only if you
1835+
pass *labels*. In other cases, please use `~.Axes.tick_params`.
1836+
1837+
Notes
1838+
-----
1839+
The mandatory expansion of the view limits is an intentional design
1840+
choice to prevent the surprise of a non-visible tick. If you need
1841+
other limits, you should set the limits explicitly after setting the
1842+
ticks.
1843+
"""
1844+
result = self._set_tick_locations(ticks, minor=minor)
1845+
if labels is not None:
1846+
self.set_ticklabels(labels, minor=minor, **kwargs)
1847+
return result
1848+
18301849
def _get_tick_boxes_siblings(self, renderer):
18311850
"""
18321851
Get the bounding boxes for this `.axis` and its siblings

lib/matplotlib/tests/test_axes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5107,6 +5107,28 @@ def test_set_get_ticklabels():
51075107
ax[1].set_yticklabels(ax[0].get_yticklabels())
51085108

51095109

5110+
@check_figures_equal(extensions=["png"])
5111+
def test_set_ticks_with_labels(fig_test, fig_ref):
5112+
"""
5113+
Test that these two are identical::
5114+
5115+
set_xticks(ticks); set_xticklabels(labels, **kwargs)
5116+
set_xticks(ticks, labels, **kwargs)
5117+
5118+
"""
5119+
ax = fig_ref.subplots()
5120+
ax.set_xticks([1, 2, 4, 6])
5121+
ax.set_xticklabels(['a', 'b', 'c', 'd'], fontweight='bold')
5122+
ax.set_yticks([1, 3, 5])
5123+
ax.set_yticks([2, 4], minor=True)
5124+
ax.set_yticklabels(['A', 'B'], minor=True)
5125+
5126+
ax = fig_test.subplots()
5127+
ax.set_xticks([1, 2, 4, 6], ['a', 'b', 'c', 'd'], fontweight='bold')
5128+
ax.set_yticks([1, 3, 5])
5129+
ax.set_yticks([2, 4], ['A', 'B'], minor=True)
5130+
5131+
51105132
def test_subsampled_ticklabels():
51115133
# test issue 11937
51125134
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)