Skip to content
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

Fix Axes clearing with Matplotlib 3.6+ #2071

Merged
merged 2 commits into from
Aug 30, 2022
Merged

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Aug 21, 2022

Rationale

Matplotlib 3.6 expired the cla/clear alias, so GeoAxes were not being correctly cleared with Matplotlib 3.6.

Implications

Plots don't get spectacularly broken.

Comment on lines 589 to 606
if mpl.__version__ >= '3.6':
def clear(self):
"""Clear the current Axes and add boundary lines."""
result = super().clear()
self._clear()
return result
else:
def cla(self):
"""Clear the current Axes and add boundary lines."""
result = super().cla()
self._clear()
return result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than explicit version gating, what about just defining clear ourselves even for older versions of MPL.

Suggested change
if mpl.__version__ >= '3.6':
def clear(self):
"""Clear the current Axes and add boundary lines."""
result = super().clear()
self._clear()
return result
else:
def cla(self):
"""Clear the current Axes and add boundary lines."""
result = super().cla()
self._clear()
return result
def clear(self):
"""Clear the current Axes and add boundary lines."""
result = getattr(super(), "clear", super().cla)()
self._clear()
return result
def cla(self):
"""Clear the current Axes and add boundary lines."""
return self.clear()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Axes.clear always existed though, so I don't think that getattr fallback will work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I agree, I don't think I realized that it was the coming back down calls that were being missed. It might be helpful to put a quick comment about removing this once we only require MPL 3.6.

@tacaswell
Copy link

This should be fixed upstream.

@dopplershift
Copy link
Contributor

@tacaswell Are you saying upstream needs to put in a fix? Or it already has? I can't find a relevant PR.

@tacaswell
Copy link

That is a prescriptive "should" as I think this is a release blocker for Matplotlib.

I do not fully understand the issue yet, but I do not think we intended to remove support for either clear or cla, just make the aliases and always do the same thing!

@dopplershift
Copy link
Contributor

@tacaswell That's what I thought, but wasn't sure.

So I just finally made sense of it too. The problem is that matplotlib itself changed all calls to use clear(), and changed cla() itself to call clear(). That breaks Cartopy because now there's no matplotlib code that will ever call Geoaxes.cla(), it's going to look for Geoaxes.clear(), which falls back to Axes.clear(), which avoids all of Cartopy's code.

@tacaswell
Copy link

Ah, so we need check in clear to see if there is a sub-class defined cla, call that and then warn?

@dopplershift
Copy link
Contributor

@tacaswell That makes sense to me. @QuLogic ?

Would definitely affect how we proceed here.

@QuLogic
Copy link
Member Author

QuLogic commented Aug 25, 2022

I've put in matplotlib/matplotlib#23735 upstream. I'm not sure that we actually want to warn about it.

However, I think we still want to move forward this this PR, so that we (eventually when Matplotlib 3.6 is the minimum required) are using the new term.

Comment on lines 589 to 606
if mpl.__version__ >= '3.6':
def clear(self):
"""Clear the current Axes and add boundary lines."""
result = super().clear()
self._clear()
return result
else:
def cla(self):
"""Clear the current Axes and add boundary lines."""
result = super().cla()
self._clear()
return result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I agree, I don't think I realized that it was the coming back down calls that were being missed. It might be helpful to put a quick comment about removing this once we only require MPL 3.6.

@dopplershift
Copy link
Contributor

I definitely agree we should go ahead and future proof, I just wasn't sure if the form of the fix here would change based on what's merged for matplotlib 3.6.

Copy link
Contributor

@greglucas greglucas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked out both development branches to test these updates out, and this is definitely close. On Cartopy main I get the expected PendingDeprecationWarning, but on this branch there are a bunch of failures. This is due to some more naming conflicts now that Matplotlib added a private _clear() method that is called and you're also redefining that here. The easiest fix is probably to update the name here to something explicit and only for Cartopy.

def cla(self):
"""Clear the current axes and adds boundary lines."""
result = super().cla()
def _clear(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _clear(self):
def _cartopy_clear(self):

and update the calls below.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we changed to __clear upstream to get the name mangling, but this is probably a good idea anyway!

@greglucas
Copy link
Contributor

I just tested this out locally with rc2 and all worked well for me.

@greglucas greglucas merged commit 2ef404b into SciTools:main Aug 30, 2022
@QuLogic QuLogic deleted the fix-mpl36 branch August 30, 2022 22:31
@QuLogic QuLogic added this to the 0.21 milestone Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants