Skip to content

Commit c30354e

Browse files
committed
Align Artist transform and Collection offset transform behaviour
1 parent c09ac03 commit c30354e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

doc/users/next_whats_new/collection_offsets.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Setting collection offset transform after initialization
22
--------------------------------------------------------
33
`~matplotlib.collections.Collection.set_offset_transform` was added.
44

5-
Previously the offset transform could not be set after initialization.
5+
Previously the offset transform could not be set after initialization. This can be helpful when creating a `Collection` outside an axes object and later adding it with `ax.add_collection` and settings the offset transform to `ax.transData`.

lib/matplotlib/collections.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class Collection(artist.Artist, cm.ScalarMappable):
6262
ignoring those that were manually passed in.
6363
"""
6464
_offsets = np.zeros((0, 2))
65-
_transOffset = transforms.IdentityTransform()
6665
#: Either a list of 3x3 arrays or an Nx3x3 array (representing N
6766
#: transforms), suitable for the `all_transforms` argument to
6867
#: `~matplotlib.backend_bases.RendererBase.draw_path_collection`;
@@ -204,8 +203,7 @@ def __init__(self,
204203
offsets = offsets[None, :]
205204
self._offsets = offsets
206205

207-
if transOffset is not None:
208-
self._transOffset = transOffset
206+
self._transOffset = transOffset
209207

210208
self._path_effects = None
211209
self.update(kwargs)
@@ -221,16 +219,22 @@ def get_transforms(self):
221219
return self._transforms
222220

223221
def get_offset_transform(self):
224-
t = self._transOffset
225-
if (not isinstance(t, transforms.Transform)
226-
and hasattr(t, '_as_mpl_transform')):
227-
t = t._as_mpl_transform(self.axes)
228-
return t
222+
"""Return the `.Transform` instance used by this artist offset."""
223+
if self._transOffset is None:
224+
self._transOffset = transforms.IdentityTransform()
225+
elif (not isinstance(self._transOffset, transforms.Transform)
226+
and hasattr(self._transOffset, '_as_mpl_transform')):
227+
self._transOffset = self._transOffset._as_mpl_transform(self.axes)
228+
return self._transOffset
229229

230230
def set_offset_transform(self, transOffset):
231+
"""
232+
Set the artist offset transform.
233+
Parameters
234+
----------
235+
transOffset : `.Transform`
236+
"""
231237
self._transOffset = transOffset
232-
self.pchanged()
233-
self.stale = True
234238

235239
def get_datalim(self, transData):
236240
# Calculate the data limits and return them as a `.Bbox`.
@@ -252,7 +256,7 @@ def get_datalim(self, transData):
252256
transform = self.get_transform()
253257
transOffset = self.get_offset_transform()
254258
hasOffsets = np.any(self._offsets) # True if any non-zero offsets
255-
if (hasOffsets and not transOffset.contains_branch(transData)):
259+
if hasOffsets and not transOffset.contains_branch(transData):
256260
# if there are offsets but in some coords other than data,
257261
# then don't use them for autoscaling.
258262
return transforms.Bbox.null()

0 commit comments

Comments
 (0)