Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efc1c87

Browse files
committed
Fix removal of shared polar axes.
There's really two separate fixes here: - Move isDefault_{maj,min}{loc,fmt} tracking to the Ticker instances, where they logically belong (note the previous need to additionally track them manually on axes removal, when that info was tracked on the Axis). This has the side effect of fixing removal of sharex'd polar axes, as ThetaLocators rely on _AxisWrappers which don't have that isDefault attribute. (Note that the patch would have resulted in a net decrease of lines of code if it didn't need to maintain backcompat on isDefault_foos). - Ensure that RadialLocator correctly propagates Axis information to the linear locator it wraps (consistently with ThetaLocator), so that when an axes is removed the wrapped linear locator doesn't stay pointing at an obsolete axes. This, together with the first patch, fixes removal of sharey'd polar axes.
1 parent c7ebf91 commit efc1c87

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

lib/matplotlib/axis.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ class Ticker:
552552
def __init__(self):
553553
self._locator = None
554554
self._formatter = None
555+
self._locator_is_default = True
556+
self._formatter_is_default = True
555557

556558
@property
557559
def locator(self):
@@ -689,6 +691,38 @@ def __init__(self, axes, pickradius=15):
689691
self.clear()
690692
self._set_scale('linear')
691693

694+
@property
695+
def isDefault_majloc(self):
696+
return self.major._locator_is_default
697+
698+
@isDefault_majloc.setter
699+
def isDefault_majloc(self, value):
700+
self.major._locator_is_default = value
701+
702+
@property
703+
def isDefault_majfmt(self):
704+
return self.major._formatter_is_default
705+
706+
@isDefault_majfmt.setter
707+
def isDefault_majfmt(self, value):
708+
self.major._formatter_is_default = value
709+
710+
@property
711+
def isDefault_minloc(self):
712+
return self.minor._locator_is_default
713+
714+
@isDefault_minloc.setter
715+
def isDefault_minloc(self, value):
716+
self.minor._locator_is_default = value
717+
718+
@property
719+
def isDefault_minfmt(self):
720+
return self.minor._formatter_is_default
721+
722+
@isDefault_minfmt.setter
723+
def isDefault_minfmt(self, value):
724+
self.minor._formatter_is_default = value
725+
692726
# During initialization, Axis objects often create ticks that are later
693727
# unused; this turns out to be a very slow step. Instead, use a custom
694728
# descriptor to make the tick lists lazy and instantiate them as needed.

lib/matplotlib/figure.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -916,33 +916,10 @@ def _reset_locators_and_formatters(axis):
916916
# Set the formatters and locators to be associated with axis
917917
# (where previously they may have been associated with another
918918
# Axis instance)
919-
#
920-
# Because set_major_formatter() etc. force isDefault_* to be False,
921-
# we have to manually check if the original formatter was a
922-
# default and manually set isDefault_* if that was the case.
923-
majfmt = axis.get_major_formatter()
924-
isDefault = majfmt.axis.isDefault_majfmt
925-
axis.set_major_formatter(majfmt)
926-
if isDefault:
927-
majfmt.axis.isDefault_majfmt = True
928-
929-
majloc = axis.get_major_locator()
930-
isDefault = majloc.axis.isDefault_majloc
931-
axis.set_major_locator(majloc)
932-
if isDefault:
933-
majloc.axis.isDefault_majloc = True
934-
935-
minfmt = axis.get_minor_formatter()
936-
isDefault = majloc.axis.isDefault_minfmt
937-
axis.set_minor_formatter(minfmt)
938-
if isDefault:
939-
minfmt.axis.isDefault_minfmt = True
940-
941-
minloc = axis.get_minor_locator()
942-
isDefault = majloc.axis.isDefault_minloc
943-
axis.set_minor_locator(minloc)
944-
if isDefault:
945-
minloc.axis.isDefault_minloc = True
919+
axis.get_major_formatter().set_axis(axis)
920+
axis.get_major_locator().set_axis(axis)
921+
axis.get_minor_formatter().set_axis(axis)
922+
axis.get_minor_locator().set_axis(axis)
946923

947924
def _break_share_link(ax, grouper):
948925
siblings = grouper.get_siblings(ax)

lib/matplotlib/projections/polar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ def __init__(self, base, axes=None):
418418
self.base = base
419419
self._axes = axes
420420

421+
def set_axis(self, axis):
422+
self.base.set_axis(axis)
423+
421424
def __call__(self):
422425
# Ensure previous behaviour with full circle non-annular views.
423426
if self._axes:

lib/matplotlib/tests/test_polar.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,18 @@ def test_thetalim_args():
357357
assert tuple(np.radians((ax.get_thetamin(), ax.get_thetamax()))) == (0, 1)
358358
ax.set_thetalim((2, 3))
359359
assert tuple(np.radians((ax.get_thetamin(), ax.get_thetamax()))) == (2, 3)
360+
361+
362+
@check_figures_equal(extensions=["png"])
363+
def test_remove_shared_polar(fig_ref, fig_test):
364+
# Removing shared polar axes used to crash. Test removing them, keeping in
365+
# both cases just the lower left axes of a grid to avoid running into a
366+
# separate issue (now being fixed) of ticklabel visibility for shared axes.
367+
axs = fig_ref.subplots(
368+
2, 2, sharex=True, subplot_kw={"projection": "polar"})
369+
for i in [0, 1, 3]:
370+
axs.flat[i].remove()
371+
axs = fig_test.subplots(
372+
2, 2, sharey=True, subplot_kw={"projection": "polar"})
373+
for i in [0, 1, 3]:
374+
axs.flat[i].remove()

0 commit comments

Comments
 (0)