Skip to content

Commit 440f761

Browse files
authored
Merge pull request matplotlib#26401 from jklymak/warn-better-ticklabels
WARN: more direct warning ticklabels
2 parents aa3e00d + dd2e6f8 commit 440f761

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/matplotlib/axis.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import functools
77
import logging
88
from numbers import Real
9+
import warnings
910

1011
import numpy as np
1112

@@ -2014,7 +2015,10 @@ def set_ticklabels(self, labels, *, minor=False, fontdict=None, **kwargs):
20142015
raise TypeError(f"{labels:=} must be a sequence") from None
20152016
locator = (self.get_minor_locator() if minor
20162017
else self.get_major_locator())
2017-
if isinstance(locator, mticker.FixedLocator):
2018+
if not labels:
2019+
# eg labels=[]:
2020+
formatter = mticker.NullFormatter()
2021+
elif isinstance(locator, mticker.FixedLocator):
20182022
# Passing [] as a list of labels is often used as a way to
20192023
# remove all tick labels, so only error for > 0 labels
20202024
if len(locator.locs) != len(labels) and len(labels) != 0:
@@ -2027,16 +2031,23 @@ def set_ticklabels(self, labels, *, minor=False, fontdict=None, **kwargs):
20272031
func = functools.partial(self._format_with_dict, tickd)
20282032
formatter = mticker.FuncFormatter(func)
20292033
else:
2034+
_api.warn_external(
2035+
"set_ticklabels() should only be used with a fixed number of "
2036+
"ticks, i.e. after set_ticks() or using a FixedLocator.")
20302037
formatter = mticker.FixedFormatter(labels)
20312038

2032-
if minor:
2033-
self.set_minor_formatter(formatter)
2034-
locs = self.get_minorticklocs()
2035-
ticks = self.get_minor_ticks(len(locs))
2036-
else:
2037-
self.set_major_formatter(formatter)
2038-
locs = self.get_majorticklocs()
2039-
ticks = self.get_major_ticks(len(locs))
2039+
with warnings.catch_warnings():
2040+
warnings.filterwarnings(
2041+
"ignore",
2042+
message="FixedFormatter should only be used together with FixedLocator")
2043+
if minor:
2044+
self.set_minor_formatter(formatter)
2045+
locs = self.get_minorticklocs()
2046+
ticks = self.get_minor_ticks(len(locs))
2047+
else:
2048+
self.set_major_formatter(formatter)
2049+
locs = self.get_majorticklocs()
2050+
ticks = self.get_major_ticks(len(locs))
20402051

20412052
ret = []
20422053
if fontdict is not None:

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6063,6 +6063,21 @@ def test_retain_tick_visibility():
60636063
ax.tick_params(axis="y", which="both", length=0)
60646064

60656065

6066+
def test_warn_too_few_labels():
6067+
# note that the axis is still using an AutoLocator:
6068+
fig, ax = plt.subplots()
6069+
with pytest.warns(
6070+
UserWarning,
6071+
match=r'set_ticklabels\(\) should only be used with a fixed number'):
6072+
ax.set_xticklabels(['0', '0.1'])
6073+
# note that the axis is still using a FixedLocator:
6074+
fig, ax = plt.subplots()
6075+
ax.set_xticks([0, 0.5, 1])
6076+
with pytest.raises(ValueError,
6077+
match='The number of FixedLocator locations'):
6078+
ax.set_xticklabels(['0', '0.1'])
6079+
6080+
60666081
def test_tick_label_update():
60676082
# test issue 9397
60686083

0 commit comments

Comments
 (0)