Skip to content

Fix Axes.clear() crash for custom spine types#31688

Merged
timhoffm merged 1 commit into
matplotlib:mainfrom
eendebakpt:fix/clear-custom-spine-type
May 17, 2026
Merged

Fix Axes.clear() crash for custom spine types#31688
timhoffm merged 1 commit into
matplotlib:mainfrom
eendebakpt:fix/clear-custom-spine-type

Conversation

@eendebakpt

Copy link
Copy Markdown
Contributor

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

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>
@rcomer rcomer added this to the v3.12.0 milestone May 16, 2026
Comment thread lib/matplotlib/spines.py
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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 timhoffm merged commit b22611b into matplotlib:main May 17, 2026
40 of 41 checks passed
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