Skip to content

Commit cfc6b1f

Browse files
authored
Merge pull request #20047 from timhoffm/set_ticks-labels
ENH: Add labels parameter to set_ticks()
2 parents 3665241 + 9e5b1d6 commit cfc6b1f

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
@@ -1706,10 +1706,16 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
17061706
r"""
17071707
Set the text values of the tick labels.
17081708
1709-
.. warning::
1710-
This method should only be used after fixing the tick positions
1711-
using `.Axis.set_ticks`. Otherwise, the labels may end up in
1712-
unexpected positions.
1709+
.. admonition:: Discouraged
1710+
1711+
The use of this method is discouraged, because of the dependency
1712+
on tick positions. In most cases, you'll want to use
1713+
``set_[x/y]ticks(positions, labels)`` instead.
1714+
1715+
If you are using this method, you should always fix the tick
1716+
positions before, e.g. by using `.Axis.set_ticks` or by explicitly
1717+
setting a `~.ticker.FixedLocator`. Otherwise, ticks are free to
1718+
move and the labels may end up in unexpected positions.
17131719
17141720
Parameters
17151721
----------
@@ -1817,27 +1823,9 @@ def _set_ticklabels(self, labels, fontdict=None, minor=False, **kwargs):
18171823
kwargs.update(fontdict)
18181824
return self.set_ticklabels(labels, minor=minor, **kwargs)
18191825

1820-
def set_ticks(self, ticks, *, minor=False):
1821-
"""
1822-
Set this Axis' tick locations.
1823-
1824-
If necessary, the view limits of the Axis are expanded so that all
1825-
given ticks are visible.
1826+
def _set_tick_locations(self, ticks, *, minor=False):
1827+
# see docstring of set_ticks
18261828

1827-
Parameters
1828-
----------
1829-
ticks : list of floats
1830-
List of tick locations.
1831-
minor : bool, default: False
1832-
If ``False``, set the major ticks; if ``True``, the minor ticks.
1833-
1834-
Notes
1835-
-----
1836-
The mandatory expansion of the view limits is an intentional design
1837-
choice to prevent the surprise of a non-visible tick. If you need
1838-
other limits, you should set the limits explicitly after setting the
1839-
ticks.
1840-
"""
18411829
# XXX if the user changes units, the information will be lost here
18421830
ticks = self.convert_units(ticks)
18431831
if self is self.axes.xaxis:
@@ -1872,6 +1860,37 @@ def set_ticks(self, ticks, *, minor=False):
18721860
self.set_major_locator(mticker.FixedLocator(ticks))
18731861
return self.get_major_ticks(len(ticks))
18741862

1863+
def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
1864+
"""
1865+
Set this Axis' tick locations and optionally labels.
1866+
1867+
If necessary, the view limits of the Axis are expanded so that all
1868+
given ticks are visible.
1869+
1870+
Parameters
1871+
----------
1872+
ticks : list of floats
1873+
List of tick locations.
1874+
labels : list of str, optional
1875+
List of tick labels. If not set, the labels show the data value.
1876+
minor : bool, default: False
1877+
If ``False``, set the major ticks; if ``True``, the minor ticks.
1878+
**kwargs
1879+
`.Text` properties for the labels. These take effect only if you
1880+
pass *labels*. In other cases, please use `~.Axes.tick_params`.
1881+
1882+
Notes
1883+
-----
1884+
The mandatory expansion of the view limits is an intentional design
1885+
choice to prevent the surprise of a non-visible tick. If you need
1886+
other limits, you should set the limits explicitly after setting the
1887+
ticks.
1888+
"""
1889+
result = self._set_tick_locations(ticks, minor=minor)
1890+
if labels is not None:
1891+
self.set_ticklabels(labels, minor=minor, **kwargs)
1892+
return result
1893+
18751894
def _get_tick_boxes_siblings(self, renderer):
18761895
"""
18771896
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
@@ -5146,6 +5146,28 @@ def test_set_get_ticklabels():
51465146
ax[1].set_yticklabels(ax[0].get_yticklabels())
51475147

51485148

5149+
@check_figures_equal(extensions=["png"])
5150+
def test_set_ticks_with_labels(fig_test, fig_ref):
5151+
"""
5152+
Test that these two are identical::
5153+
5154+
set_xticks(ticks); set_xticklabels(labels, **kwargs)
5155+
set_xticks(ticks, labels, **kwargs)
5156+
5157+
"""
5158+
ax = fig_ref.subplots()
5159+
ax.set_xticks([1, 2, 4, 6])
5160+
ax.set_xticklabels(['a', 'b', 'c', 'd'], fontweight='bold')
5161+
ax.set_yticks([1, 3, 5])
5162+
ax.set_yticks([2, 4], minor=True)
5163+
ax.set_yticklabels(['A', 'B'], minor=True)
5164+
5165+
ax = fig_test.subplots()
5166+
ax.set_xticks([1, 2, 4, 6], ['a', 'b', 'c', 'd'], fontweight='bold')
5167+
ax.set_yticks([1, 3, 5])
5168+
ax.set_yticks([2, 4], ['A', 'B'], minor=True)
5169+
5170+
51495171
def test_subsampled_ticklabels():
51505172
# test issue 11937
51515173
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)