Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ddee963

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

File tree

4 files changed

+82
-32
lines changed

4 files changed

+82
-32
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,21 +124,12 @@ 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+
def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
128+
ret = self._axis.set_ticks(ticks, labels, minor=minor, **kwargs)
138129
self.stale = True
139130
self._ticks_set = True
140131
return ret
132+
set_ticks.__doc__ = Axis.set_ticks.__doc__
141133

142134
def set_functions(self, functions):
143135
"""

lib/matplotlib/axis.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,14 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
16611661
r"""
16621662
Set the text values of the tick labels.
16631663
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+
16641670
.. warning::
1671+
16651672
This method should only be used after fixing the tick positions
16661673
using `.Axis.set_ticks`. Otherwise, the labels may end up in
16671674
unexpected positions.
@@ -1772,27 +1779,9 @@ def _set_ticklabels(self, labels, fontdict=None, minor=False, **kwargs):
17721779
kwargs.update(fontdict)
17731780
return self.set_ticklabels(labels, minor=minor, **kwargs)
17741781

1775-
def set_ticks(self, ticks, *, minor=False):
1776-
"""
1777-
Set this Axis' tick locations.
1782+
def _set_tick_locations(self, ticks, *, minor=True):
1783+
# see docstring of set_ticks
17781784

1779-
If necessary, the view limits of the Axis are expanded so that all
1780-
given ticks are visible.
1781-
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-
"""
17961785
# XXX if the user changes units, the information will be lost here
17971786
ticks = self.convert_units(ticks)
17981787
if self is self.axes.xaxis:
@@ -1827,6 +1816,37 @@ def set_ticks(self, ticks, *, minor=False):
18271816
self.set_major_locator(mticker.FixedLocator(ticks))
18281817
return self.get_major_ticks(len(ticks))
18291818

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