Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,9 @@ def _set_formatter(self, formatter, level):

if (isinstance(formatter, mticker.FixedFormatter)
and len(formatter.seq) > 0
and not isinstance(level.locator, mticker.FixedLocator)):
and not isinstance(level.locator, mticker.FixedLocator)
and not (hasattr(level.locator, 'base') and
isinstance(level.locator.base, mticker.FixedLocator))):
_api.warn_external('FixedFormatter should only be used together '
'with FixedLocator')

Expand Down Expand Up @@ -2142,16 +2144,21 @@ def set_ticklabels(self, labels, *, minor=False, fontdict=None, **kwargs):
if not labels:
# eg labels=[]:
formatter = mticker.NullFormatter()
elif isinstance(locator, mticker.FixedLocator):
elif (isinstance(locator, mticker.FixedLocator) or
(hasattr(locator, 'base') and
isinstance(locator.base, mticker.FixedLocator))):
# Also handles locators that wrap a FixedLocator (e.g. RadialLocator).
fixed_locator = (locator if isinstance(locator, mticker.FixedLocator)
else locator.base)
# Passing [] as a list of labels is often used as a way to
# remove all tick labels, so only error for > 0 labels
if len(locator.locs) != len(labels) and len(labels) != 0:
if len(fixed_locator.locs) != len(labels) and len(labels) != 0:
raise ValueError(
"The number of FixedLocator locations"
f" ({len(locator.locs)}), usually from a call to"
f" ({len(fixed_locator.locs)}), usually from a call to"
" set_ticks, does not match"
f" the number of labels ({len(labels)}).")
tickd = {loc: lab for loc, lab in zip(locator.locs, labels)}
tickd = {loc: lab for loc, lab in zip(fixed_locator.locs, labels)}
func = functools.partial(self._format_with_dict, tickd)
formatter = mticker.FuncFormatter(func)
else:
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,13 @@ def test_radial_locator_wrapping():
assert ax.yaxis.isDefault_majloc
assert isinstance(ax.yaxis.get_major_locator(), RadialLocator)
assert isinstance(ax.yaxis.get_major_locator().base, mticker.LogLocator)


def test_set_rticks_ticklabels_no_warning():
# Regression test: RadialLocator wrapping a FixedLocator must not trigger
# the "set_ticklabels() should only be used with a fixed number of ticks"
# UserWarning when set_ticks()/set_rticks() was called first.

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.set_rticks([0, 1, 2, 3])
ax.yaxis.set_ticklabels(['zero', 'one', 'two', 'three'])
Loading