Skip to content

Commit 1663e9a

Browse files
authored
Merge pull request #30174 from timhoffm/fix-radialaxis
FIX: Ensure Locators on RadialAxis are always correctly wrapped
2 parents 30f4ece + b02ed41 commit 1663e9a

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

lib/matplotlib/projections/polar.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -679,20 +679,15 @@ def __init__(self, *args, **kwargs):
679679
super().__init__(*args, **kwargs)
680680
self.sticky_edges.y.append(0)
681681

682-
def _wrap_locator_formatter(self):
683-
self.set_major_locator(RadialLocator(self.get_major_locator(),
684-
self.axes))
685-
self.isDefault_majloc = True
682+
def set_major_locator(self, locator):
683+
if not isinstance(locator, RadialLocator):
684+
locator = RadialLocator(locator, self.axes)
685+
super().set_major_locator(locator)
686686

687687
def clear(self):
688688
# docstring inherited
689689
super().clear()
690690
self.set_ticks_position('none')
691-
self._wrap_locator_formatter()
692-
693-
def _set_scale(self, value, **kwargs):
694-
super()._set_scale(value, **kwargs)
695-
self._wrap_locator_formatter()
696691

697692

698693
def _is_full_circle_deg(thetamin, thetamax):
@@ -1248,19 +1243,11 @@ def set_rlabel_position(self, value):
12481243
"""
12491244
self._r_label_position.clear().translate(np.deg2rad(value), 0.0)
12501245

1251-
def set_yscale(self, *args, **kwargs):
1252-
super().set_yscale(*args, **kwargs)
1253-
self.yaxis.set_major_locator(
1254-
self.RadialLocator(self.yaxis.get_major_locator(), self))
1255-
12561246
def set_rscale(self, *args, **kwargs):
12571247
return Axes.set_yscale(self, *args, **kwargs)
12581248

12591249
def set_rticks(self, *args, **kwargs):
1260-
result = Axes.set_yticks(self, *args, **kwargs)
1261-
self.yaxis.set_major_locator(
1262-
self.RadialLocator(self.yaxis.get_major_locator(), self))
1263-
return result
1250+
return Axes.set_yticks(self, *args, **kwargs)
12641251

12651252
def set_thetagrids(self, angles, labels=None, fmt=None, **kwargs):
12661253
"""

lib/matplotlib/tests/test_polar.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import pytest
44

55
import matplotlib as mpl
6+
from matplotlib.projections.polar import RadialLocator
67
from matplotlib import pyplot as plt
78
from matplotlib.testing.decorators import image_comparison, check_figures_equal
9+
import matplotlib.ticker as mticker
810

911

1012
@image_comparison(['polar_axes.png'], style='default', tol=0.012)
@@ -546,3 +548,43 @@ def test_radial_limits_behavior():
546548
# negative data also autoscales to negative limits
547549
ax.plot([1, 2], [-1, -2])
548550
assert ax.get_ylim() == (-2, 2)
551+
552+
553+
def test_radial_locator_wrapping():
554+
# Check that the locator is always wrapped inside a RadialLocator
555+
# and that RaidialAxis.isDefault_majloc is set correctly.
556+
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
557+
assert ax.yaxis.isDefault_majloc
558+
assert isinstance(ax.yaxis.get_major_locator(), RadialLocator)
559+
560+
# set an explicit locator
561+
locator = mticker.MaxNLocator(3)
562+
ax.yaxis.set_major_locator(locator)
563+
assert not ax.yaxis.isDefault_majloc
564+
assert isinstance(ax.yaxis.get_major_locator(), RadialLocator)
565+
assert ax.yaxis.get_major_locator().base is locator
566+
567+
ax.clear() # reset to the default locator
568+
assert ax.yaxis.isDefault_majloc
569+
assert isinstance(ax.yaxis.get_major_locator(), RadialLocator)
570+
571+
ax.set_rticks([0, 1, 2, 3]) # implicitly sets a FixedLocator
572+
assert not ax.yaxis.isDefault_majloc # because of the fixed ticks
573+
assert isinstance(ax.yaxis.get_major_locator(), RadialLocator)
574+
assert isinstance(ax.yaxis.get_major_locator().base, mticker.FixedLocator)
575+
576+
ax.clear()
577+
578+
ax.set_rgrids([0, 1, 2, 3]) # implicitly sets a FixedLocator
579+
assert not ax.yaxis.isDefault_majloc # because of the fixed ticks
580+
assert isinstance(ax.yaxis.get_major_locator(), RadialLocator)
581+
assert isinstance(ax.yaxis.get_major_locator().base, mticker.FixedLocator)
582+
583+
ax.clear()
584+
585+
ax.set_yscale("log") # implicitly sets a LogLocator
586+
# Note that the LogLocator is still considered the default locator
587+
# for the log scale
588+
assert ax.yaxis.isDefault_majloc
589+
assert isinstance(ax.yaxis.get_major_locator(), RadialLocator)
590+
assert isinstance(ax.yaxis.get_major_locator().base, mticker.LogLocator)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy