Fix Axes.clear() crash for custom spine types#31688
Merged
Merged
Conversation
PR matplotlib#31525 made Axes.__clear call Spine._ensure_transform_is_set on every spine, which calls set_position(('outward', 0.0)) for a spine that still carries the placeholder transform from Spine.__init__. Custom spines such as cartopy's GeoSpine have a non-cartesian spine_type, manage their own transform, and may reject set_position, so this raised NotImplementedError on plain subplot creation. Restrict the spine nudge to the four standard cartesian spine types, the only ones set_position / get_spine_transform support. See SciTools/cartopy#2674. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
greglucas
approved these changes
May 17, 2026
| if (self.spine_type in ('left', 'right', 'top', 'bottom') | ||
| and self._position is None | ||
| and self._transform is self.axes.transData): | ||
| self.set_position(('outward', 0.0)) |
Contributor
There was a problem hiding this comment.
I suppose an alternative question would be whether there is a way to do this same idea through a different call mechanism rather than adding these cutouts for the spine types? i.e. using some different function from set_position() to ensure the transform is set.
Member
There was a problem hiding this comment.
The semantics is encapsulated in the method _ensure_transform_is_set(). Therefore special-casing in there is ok. Possibly one could require thagt derived spines must implement set_position or override _ensure_transform_is_set. But it's easy to do this on our side. If you want to improve further, there should be documentation on subclassing spines.
timhoffm
approved these changes
May 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
PR #31525 added a spine-transform nudge in Axes.__clear that calls Spine._ensure_transform_is_set() on every spine. For a spine still carrying the placeholder transform from
Spine.__init__, this calls set_position(('outward', 0.0)).Custom spines with a non-cartesian spine_type — e.g. cartopy's GeoSpine — manage their own transform and may reject set_position(), so this raised NotImplementedError on plain subplot creation (SciTools/cartopy#2674).
The nudge now only fires for the four standard cartesian spine types (left, right, top, bottom), the only ones set_position / get_spine_transform support.
AI Disclosure
Claude code was used in creating a PR.
PR checklist