Skip to content
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
10 changes: 7 additions & 3 deletions lib/matplotlib/spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ def _ensure_position_is_set(self):

def _ensure_transform_is_set(self):
# Install the default blended transform if the spine still carries
# the placeholder from Spine.__init__. No-op for spines whose
# transform was set explicitly (e.g. via set_patch_arc/_circle).
if self._position is None and self._transform is self.axes.transData:
# the placeholder from Spine.__init__. Restricted to the standard
# cartesian spines: set_position/get_spine_transform only support
# those, and other spines (polar, cartopy's GeoSpine) manage their
# own transform.
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.


def register_axis(self, axis):
Expand Down
19 changes: 18 additions & 1 deletion lib/matplotlib/tests/test_spines.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
import pytest

import matplotlib.path as mpath
import matplotlib.pyplot as plt
from matplotlib.spines import Spines
from matplotlib.spines import Spine, Spines
from matplotlib.testing.decorators import check_figures_equal, image_comparison


Expand Down Expand Up @@ -197,3 +198,19 @@ def test_spine_set_bounds_with_none():
"left bound should be numeric"
assert np.isclose(left_bound[0], ylim[0]), "Lower bound should match original value"
assert np.isclose(left_bound[1], ylim[1]), "Upper bound should match original value"


def test_clear_with_custom_spine_type():
# Spines with a non-cartesian spine_type (e.g. cartopy's GeoSpine) manage
# their own transform and may reject set_position(); Axes.clear() must not
# call _ensure_transform_is_set() on them. See SciTools/cartopy#2674.
class NoPositionSpine(Spine):
def __init__(self, axes, **kwargs):
super().__init__(axes, 'geo', mpath.Path(np.empty((0, 2))), **kwargs)

def set_position(self, position):
raise NotImplementedError('spine does not support set_position')

fig, ax = plt.subplots()
ax.spines['geo'] = NoPositionSpine(ax)
ax.clear() # must not raise
Loading