Skip to content

Commit 1c62516

Browse files
beelauuumeeseeksmachine
authored andcommitted
Backport PR matplotlib#31577: FIX: Polar Radial Tick Warnings Labels Bug
1 parent 30e22d8 commit 1c62516

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

lib/matplotlib/axis.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,9 @@ def _set_formatter(self, formatter, level):
19991999

20002000
if (isinstance(formatter, mticker.FixedFormatter)
20012001
and len(formatter.seq) > 0
2002-
and not isinstance(level.locator, mticker.FixedLocator)):
2002+
and not isinstance(level.locator, mticker.FixedLocator)
2003+
and not (hasattr(level.locator, 'base') and
2004+
isinstance(level.locator.base, mticker.FixedLocator))):
20032005
_api.warn_external('FixedFormatter should only be used together '
20042006
'with FixedLocator')
20052007

@@ -2142,16 +2144,21 @@ def set_ticklabels(self, labels, *, minor=False, fontdict=None, **kwargs):
21422144
if not labels:
21432145
# eg labels=[]:
21442146
formatter = mticker.NullFormatter()
2145-
elif isinstance(locator, mticker.FixedLocator):
2147+
elif (isinstance(locator, mticker.FixedLocator) or
2148+
(hasattr(locator, 'base') and
2149+
isinstance(locator.base, mticker.FixedLocator))):
2150+
# Also handles locators that wrap a FixedLocator (e.g. RadialLocator).
2151+
fixed_locator = (locator if isinstance(locator, mticker.FixedLocator)
2152+
else locator.base)
21462153
# Passing [] as a list of labels is often used as a way to
21472154
# remove all tick labels, so only error for > 0 labels
2148-
if len(locator.locs) != len(labels) and len(labels) != 0:
2155+
if len(fixed_locator.locs) != len(labels) and len(labels) != 0:
21492156
raise ValueError(
21502157
"The number of FixedLocator locations"
2151-
f" ({len(locator.locs)}), usually from a call to"
2158+
f" ({len(fixed_locator.locs)}), usually from a call to"
21522159
" set_ticks, does not match"
21532160
f" the number of labels ({len(labels)}).")
2154-
tickd = {loc: lab for loc, lab in zip(locator.locs, labels)}
2161+
tickd = {loc: lab for loc, lab in zip(fixed_locator.locs, labels)}
21552162
func = functools.partial(self._format_with_dict, tickd)
21562163
formatter = mticker.FuncFormatter(func)
21572164
else:

lib/matplotlib/tests/test_polar.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,13 @@ def test_radial_locator_wrapping():
591591
assert ax.yaxis.isDefault_majloc
592592
assert isinstance(ax.yaxis.get_major_locator(), RadialLocator)
593593
assert isinstance(ax.yaxis.get_major_locator().base, mticker.LogLocator)
594+
595+
596+
def test_set_rticks_ticklabels_no_warning():
597+
# Regression test: RadialLocator wrapping a FixedLocator must not trigger
598+
# the "set_ticklabels() should only be used with a fixed number of ticks"
599+
# UserWarning when set_ticks()/set_rticks() was called first.
600+
601+
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
602+
ax.set_rticks([0, 1, 2, 3])
603+
ax.yaxis.set_ticklabels(['zero', 'one', 'two', 'three'])

0 commit comments

Comments
 (0)