Skip to content

Fix removal of shared polar axes. #20009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2021
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
34 changes: 34 additions & 0 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ class Ticker:
def __init__(self):
self._locator = None
self._formatter = None
self._locator_is_default = True
self._formatter_is_default = True

@property
def locator(self):
Expand Down Expand Up @@ -689,6 +691,38 @@ def __init__(self, axes, pickradius=15):
self.clear()
self._set_scale('linear')

@property
def isDefault_majloc(self):
return self.major._locator_is_default

@isDefault_majloc.setter
def isDefault_majloc(self, value):
self.major._locator_is_default = value

@property
def isDefault_majfmt(self):
return self.major._formatter_is_default

@isDefault_majfmt.setter
def isDefault_majfmt(self, value):
self.major._formatter_is_default = value

@property
def isDefault_minloc(self):
return self.minor._locator_is_default

@isDefault_minloc.setter
def isDefault_minloc(self, value):
self.minor._locator_is_default = value

@property
def isDefault_minfmt(self):
return self.minor._formatter_is_default

@isDefault_minfmt.setter
def isDefault_minfmt(self, value):
self.minor._formatter_is_default = value

# During initialization, Axis objects often create ticks that are later
# unused; this turns out to be a very slow step. Instead, use a custom
# descriptor to make the tick lists lazy and instantiate them as needed.
Expand Down
31 changes: 4 additions & 27 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,33 +916,10 @@ def _reset_locators_and_formatters(axis):
# Set the formatters and locators to be associated with axis
# (where previously they may have been associated with another
# Axis instance)
#
# Because set_major_formatter() etc. force isDefault_* to be False,
# we have to manually check if the original formatter was a
# default and manually set isDefault_* if that was the case.
majfmt = axis.get_major_formatter()
isDefault = majfmt.axis.isDefault_majfmt
axis.set_major_formatter(majfmt)
if isDefault:
majfmt.axis.isDefault_majfmt = True

majloc = axis.get_major_locator()
isDefault = majloc.axis.isDefault_majloc
axis.set_major_locator(majloc)
if isDefault:
majloc.axis.isDefault_majloc = True

minfmt = axis.get_minor_formatter()
isDefault = majloc.axis.isDefault_minfmt
axis.set_minor_formatter(minfmt)
if isDefault:
minfmt.axis.isDefault_minfmt = True

minloc = axis.get_minor_locator()
isDefault = majloc.axis.isDefault_minloc
axis.set_minor_locator(minloc)
if isDefault:
minloc.axis.isDefault_minloc = True
axis.get_major_formatter().set_axis(axis)
axis.get_major_locator().set_axis(axis)
axis.get_minor_formatter().set_axis(axis)
axis.get_minor_locator().set_axis(axis)

def _break_share_link(ax, grouper):
siblings = grouper.get_siblings(ax)
Expand Down
3 changes: 3 additions & 0 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ def __init__(self, base, axes=None):
self.base = base
self._axes = axes

def set_axis(self, axis):
self.base.set_axis(axis)

def __call__(self):
# Ensure previous behaviour with full circle non-annular views.
if self._axes:
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,18 @@ def test_axvspan():
ax = plt.subplot(projection="polar")
span = plt.axvspan(0, np.pi/4)
assert span.get_path()._interpolation_steps > 1


@check_figures_equal(extensions=["png"])
def test_remove_shared_polar(fig_ref, fig_test):
# Removing shared polar axes used to crash. Test removing them, keeping in
# both cases just the lower left axes of a grid to avoid running into a
# separate issue (now being fixed) of ticklabel visibility for shared axes.
axs = fig_ref.subplots(
2, 2, sharex=True, subplot_kw={"projection": "polar"})
for i in [0, 1, 3]:
axs.flat[i].remove()
axs = fig_test.subplots(
2, 2, sharey=True, subplot_kw={"projection": "polar"})
for i in [0, 1, 3]:
axs.flat[i].remove()