Skip to content

Commit 63bf548

Browse files
committed
Use FixedFormatter only with FixedLocator
1 parent ea79c2a commit 63bf548

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,10 +3412,15 @@ def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs):
34123412
"""
34133413
Set the x-tick labels with list of string labels.
34143414
3415+
.. warning::
3416+
This method should only be used after fixing the tick positions
3417+
using `~.axes.Axes.set_xticks`. Otherwise, the labels may end up
3418+
in unexpected positions.
3419+
34153420
Parameters
34163421
----------
3417-
labels : List[str]
3418-
List of string labels.
3422+
labels : list of str
3423+
The label texts.
34193424
34203425
fontdict : dict, optional
34213426
A dictionary controlling the appearance of the ticklabels.
@@ -3426,12 +3431,13 @@ def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs):
34263431
'verticalalignment': 'baseline',
34273432
'horizontalalignment': loc}
34283433
3429-
minor : bool, optional
3434+
minor : bool, default: False
34303435
Whether to set the minor ticklabels rather than the major ones.
34313436
34323437
Returns
34333438
-------
3434-
A list of `~.text.Text` instances.
3439+
labels
3440+
A list of `~.text.Text` instances.
34353441
34363442
Other Parameters
34373443
-----------------
@@ -3792,12 +3798,17 @@ def get_yticklabels(self, minor=False, which=None):
37923798

37933799
def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):
37943800
"""
3795-
Set the y-tick labels with list of strings labels.
3801+
Set the y-tick labels with list of string labels.
3802+
3803+
.. warning::
3804+
This method should only be used after fixing the tick positions
3805+
using `~.axes.Axes.set_yticks`. Otherwise, the labels may end up
3806+
in unexpected positions.
37963807
37973808
Parameters
37983809
----------
3799-
labels : List[str]
3800-
list of string labels
3810+
labels : list of str
3811+
The label texts.
38013812
38023813
fontdict : dict, optional
38033814
A dictionary controlling the appearance of the ticklabels.
@@ -3808,12 +3819,13 @@ def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):
38083819
'verticalalignment': 'baseline',
38093820
'horizontalalignment': loc}
38103821
3811-
minor : bool, optional
3822+
minor : bool, default: False
38123823
Whether to set the minor ticklabels rather than the major ones.
38133824
38143825
Returns
38153826
-------
3816-
A list of `~.text.Text` instances.
3827+
labels
3828+
A list of `~.text.Text` instances.
38173829
38183830
Other Parameters
38193831
----------------

lib/matplotlib/axis.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import datetime
66
import logging
7+
import warnings
78

89
import numpy as np
910

@@ -1632,6 +1633,10 @@ def set_major_formatter(self, formatter):
16321633
formatter : `~matplotlib.ticker.Formatter`
16331634
"""
16341635
cbook._check_isinstance(mticker.Formatter, formatter=formatter)
1636+
if (isinstance(formatter, mticker.FixedFormatter)
1637+
and not isinstance(self.major.locator, mticker.FixedLocator)):
1638+
warnings.warn('FixedFormatter should only be used together with '
1639+
'FixedLocator', stacklevel=2)
16351640
self.isDefault_majfmt = False
16361641
self.major.formatter = formatter
16371642
formatter.set_axis(self)
@@ -1646,6 +1651,10 @@ def set_minor_formatter(self, formatter):
16461651
formatter : `~matplotlib.ticker.Formatter`
16471652
"""
16481653
cbook._check_isinstance(mticker.Formatter, formatter=formatter)
1654+
if (isinstance(formatter, mticker.FixedFormatter)
1655+
and not isinstance(self.major.locator, mticker.FixedLocator)):
1656+
warnings.warn('FixedFormatter should only be used together with '
1657+
'FixedLocator', stacklevel=2)
16491658
self.isDefault_minfmt = False
16501659
self.minor.formatter = formatter
16511660
formatter.set_axis(self)
@@ -1697,6 +1706,11 @@ def set_ticklabels(self, ticklabels, *args, minor=False, **kwargs):
16971706
r"""
16981707
Set the text values of the tick labels.
16991708
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.
1713+
17001714
Parameters
17011715
----------
17021716
ticklabels : sequence of str or of `Text`\s
@@ -1718,18 +1732,8 @@ def set_ticklabels(self, ticklabels, *args, minor=False, **kwargs):
17181732
"3.1", message="Additional positional arguments to "
17191733
"set_ticklabels are ignored, and deprecated since Matplotlib "
17201734
"3.1; passing them will raise a TypeError in Matplotlib 3.3.")
1721-
get_labels = []
1722-
for t in ticklabels:
1723-
# try calling get_text() to check whether it is Text object
1724-
# if it is Text, get label content
1725-
try:
1726-
get_labels.append(t.get_text())
1727-
# otherwise add the label to the list directly
1728-
except AttributeError:
1729-
get_labels.append(t)
1730-
# replace the ticklabels list with the processed one
1731-
ticklabels = get_labels
1732-
1735+
ticklabels = [t.get_text() if hasattr(t, 'get_text') else t
1736+
for t in ticklabels]
17331737
if minor:
17341738
self.set_minor_formatter(mticker.FixedFormatter(ticklabels))
17351739
ticks = self.get_minor_ticks()

lib/matplotlib/ticker.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ def __call__(self, x, pos=None):
345345
class FixedFormatter(Formatter):
346346
"""
347347
Return fixed strings for tick labels based only on position, not value.
348+
349+
.. note::
350+
`.FixedFormatter` should only be used together with `.FixedLocator`.
351+
Otherwise, the labels may end up in unexpected positions.
352+
348353
"""
349354
def __init__(self, seq):
350355
"""

0 commit comments

Comments
 (0)