8000 Merge pull request #13696 from anntzer/offset_position-data · matplotlib/matplotlib@a4378ef · GitHub
[go: up one dir, main page]

Skip to content

Commit a4378ef

Browse files
authored
Merge pull request #13696 from anntzer/offset_position-data
Deprecate offset_position="data".
2 parents f091a4d + 4a569ce commit a4378ef

File tree

7 files changed

+85
-16
lines changed

7 files changed

+85
-16
lines changed

doc/api/api_changes_3.3/deprecations.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,15 @@ is deprecated; use the explicit defaults of 1 and 0, respectively, instead.
475475
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
476476
... is deprecated. Scale ``Figure.dpi_scale_trans`` by 1/72 to achieve the
477477
same effect.
478+
479+
``offset_position`` property of `.Collection`
480+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
481+
The ``offset_position`` property of `.Collection` is deprecated. In the
482+
future, `.Collection`\s will always behave as if ``offset_position`` is set to
483+
"screen" (the default).
484+
485+
Support for passing ``offset_position="data"`` to the ``draw_path_collection``
486+
of all renderer classes is deprecated.
487+
488+
`.transforms.AffineDeltaTransform` can be used as a replacement. This API is
489+
experimental and may change in the future.

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4816,8 +4816,7 @@ def reduce_C_function(C: array) -> float
48164816
edgecolors=edgecolors,
48174817
linewidths=linewidths,
48184818
offsets=offsets,
4819-
transOffset=mtransforms.IdentityTransform(),
4820-
offset_position="data"
4819+
transOffset=mtransforms.AffineDeltaTransform(self.transData),
48214820
)
48224821

48234822
# Set normalizer if bins is 'log'

lib/matplotlib/backend_bases.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
205205
*linestyles* and *antialiaseds*. *offsets* is a list of
206206
offsets to apply to each of the paths. The offsets in
207207
*offsets* are first transformed by *offsetTrans* before being
208-
applied. *offset_position* may be either "screen" or "data"
209-
depending on the space that the offsets are in.
208+
applied.
209+
210+
*offset_position* may be either "screen" or "data" depending on the
211+
space that the offsets are in; "data" is deprecated.
210212
211213
This provides a fallback implementation of
212214
:meth:`draw_path_collection` that makes multiple calls to
@@ -380,6 +382,11 @@ def _iter_collection(self, gc, master_transform, all_transforms,
380382
Naa = len(antialiaseds)
381383
Nurls = len(urls)
382384

385+
if offset_position == "data":
386+
cbook.warn_deprecated(
387+
"3.3", message="Support for offset_position='data' is "
388+
"deprecated since %(since)s and will be removed %(removal)s.")
389+
383390
if (Nfacecolors == 0 and Nedgecolors == 0) or Npaths == 0:
384391
r 67E6 eturn
385392
if Noffsets:

lib/matplotlib/backends/backend_agg.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ def _update_methods(self):
110110
self.draw_gouraud_triangles = self._renderer.draw_gouraud_triangles
111111
self.draw_image = self._renderer.draw_image
112112
self.draw_markers = self._renderer.draw_markers
113-
self.draw_path_collection = self._renderer.draw_path_collection
113+
# This is its own method for the duration of the deprecation of
114+
# offset_position = "data".
115+
# self.draw_path_collection = self._renderer.draw_path_collection
114116
self.draw_quad_mesh = self._renderer.draw_quad_mesh
115117
self.copy_from_bbox = self._renderer.copy_from_bbox
116118
self.get_content_extents = self._renderer.get_content_extents
@@ -155,6 +157,19 @@ def draw_path(self, gc, path, transform, rgbFace=None):
155157
raise OverflowError("Exceeded cell block limit (set "
156158
"'agg.path.chunksize' rcparam)") from err
157159

160+
def draw_path_collection(self, gc, master_transform, paths, all_transforms,
161+
offsets, offsetTrans, facecolors, edgecolors,
162+
linewidths, linestyles, antialiaseds, urls,
163+
offset_position):
164+
if offset_position == "data":
165+
cbook.warn_deprecated(
166+
"3.3", message="Support for offset_position='data' is "
167+
"deprecated since %(since)s and will be removed %(removal)s.")
168+
return self._renderer.draw_path_collection(
169+
gc, master_transform, paths, all_transforms, offsets, offsetTrans,
170+
facecolors, edgecolors, linewidths, linestyles, antialiaseds, urls,
171+
offset_position)
172+
158173
def draw_mathtext(self, gc, x, y, s, prop, angle):
159174
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
160175
ox, oy, width, height, descent, font_image, used_characters = \

lib/matplotlib/collections.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
4949
- *antialiaseds*: None
5050
- *offsets*: None
5151
- *transOffset*: transforms.IdentityTransform()
52-
- *offset_position*: 'screen' (default) or 'data'
52+
- *offset_position* (deprecated): 'screen' (default) or 'data' (deprecated)
5353
- *norm*: None (optional for `matplotlib.cm.ScalarMappable`)
5454
- *cmap*: None (optional for `matplotlib.cm.ScalarMappable`)
5555
- *hatch*: None
@@ -59,8 +59,8 @@ class Collection(artist.Artist, cm.ScalarMappable):
5959
rendering (default no offsets). If offset_position is 'screen'
6060
(default) the offset is applied after the master transform has
6161
been applied, that is, the offsets are in screen coordinates. If
62-
offset_position is 'data', the offset is applied before the master
63-
transform, i.e., the offsets are in data coordinates.
62+
offset_position is 'data' (deprecated), the offset is applied before the
63+
master transform, i.e., the offsets are in data coordinates.
6464
6565
If any of *edgecolors*, *facecolors*, *linewidths*, *antialiaseds* are
6666
None, they default to their `.rcParams` patch setting, in sequence form.
@@ -85,6 +85,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
8585
# subclass-by-subclass basis.
8686
_edge_default = False
8787

88+
@cbook._delete_parameter("3.3", "offset_position")
8889
def __init__(self,
8990
edgecolors=None,
9091
facecolors=None,
@@ -130,7 +131,9 @@ def __init__(self,
130131
self.set_pickradius(pickradius)
131132
self.set_urls(urls)
132133
self.set_hatch(hatch)
133-
self.set_offset_position(offset_position)
134+
10000 self._offset_position = "screen"
135+
if offset_position != "screen":
136+
self.set_offset_position(offset_position) # emit deprecation.
134137
self.set_zorder(zorder)
135138

136139
if capstyle:
@@ -404,7 +407,7 @@ def contains(self, mouseevent):
404407
self._picker is not True # the bool, not just nonzero or 1
405408
else self._pickradius)
406409

407-
if self.axes and self.get_offset_position() == "data":
410+
if self.axes:
408411
self.axes._unstale_viewLim()
409412

410413
transform, transOffset, offsets, paths = self._prepare_points()
@@ -413,7 +416,7 @@ def contains(self, mouseevent):
413416
mouseevent.x, mouseevent.y, pickradius,
414417
transform.frozen(), paths, self.get_transforms(),
415418
offsets, transOffset, pickradius <= 0,
416-
self.get_offset_position())
419+
self._offset_position)
417420

418421
return len(ind) > 0, dict(ind=ind)
419422

@@ -494,6 +497,7 @@ def get_offsets(self):
494497
else:
495498
return self._uniform_offsets
496499

500+
@cbook.deprecated("3.3")
497501
def set_offset_position(self, offset_position):
498502
"""
499503
Set how offsets are applied. If *offset_position* is 'screen'
@@ -511,6 +515,7 @@ def set_offset_position(self, offset_position):
511515
self._offset_position = offset_position
512516
self.stale = True
513517

518+
@cbook.deprecated("3.3")
514519
def get_offset_position(self):
515520
"""
516521
Return how offsets are applied for the collection. If

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ def check(master_transform, paths, all_transforms,
2626
master_transform, paths, all_transforms))
2727
gc = rb.new_gc()
2828
ids = [path_id for xo, yo, path_id, gc0, rgbFace in
29-
rb._iter_collection(gc, master_transform, all_transforms,
30-
range(len(raw_paths)), offsets,
31-
transforms.IdentityTransform(),
32-
facecolors, edgecolors, [], [], [False],
33-
[], 'data')]
29+
rb._iter_collection(
30+
gc, master_transform, all_transforms,
31+
range(len(raw_paths)), offsets,
32+
transforms.AffineDeltaTransform(master_transform),
33+
facecolors, edgecolors, [], [], [False],
34+
[], 'screen')]
3435
uses = rb._iter_collection_uses_per_path(
3536
paths, all_transforms, offsets, facecolors, edgecolors)
3637
if raw_paths:

lib/matplotlib/transforms.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,36 @@ def get_matrix(self):
26172617
return self._mtx
26182618

26192619

2620+
class AffineDeltaTransform(Affine2DBase):
2621+
r"""
2622+
A transform wrapper for transforming displacements between pairs of points.
2623+
2624+
This class is intended to be used to transform displacements ("position
2625+
deltas") between pairs of points (e.g., as the ``offset_transform``
2626+
of `.Collection`\s): given a transform ``t`` such that ``t =
2627+
AffineDeltaTransform(t) + offset``, ``AffineDeltaTransform``
2628+
satisfies ``AffineDeltaTransform(a - b) == AffineDeltaTransform(a) -
2629+
AffineDeltaTransform(b)``.
2630+
2631+
This is implemented by forcing the offset components of the transform
2632+
matrix to zero.
2633+
2634+
This class is experimental as of 3.3, and the API may change.
2635+
"""
2636+
2637+
def __init__(self, transform, **kwargs):
2638+
super().__init__(**kwargs)
2639+
self._base_transform = transform
2640+
2641+
__str__ = _make_str_method("_base_transform")
2642+
2643+
def get_matrix(self):
2644+
if self._invalid:
2645+
self._mtx = self._base_transform.get_matrix().copy()
2646+
self._mtx[:2, -1] = 0
2647+
return self._mtx
2648+
2649+
26202650
class TransformedPath(TransformNode):
26212651
"""
26222652
A `TransformedPath` caches a non-affine transformed copy of the

0 commit comments

Comments
 (0)
0