Skip to content

Commit b983b4e

Browse files
committed
Define RectangleSelector in display coords
1 parent 5aa85d6 commit b983b4e

File tree

3 files changed

+391
-278
lines changed

3 files changed

+391
-278
lines changed

lib/matplotlib/patches.py

+38-22
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,6 @@ def __init__(self, xy, width, height, angle=0.0, *,
736736
self._height = height
737737
self.angle = float(angle)
738738
self.rotation_point = rotation_point
739-
# Required for RectangleSelector with axes aspect ratio != 1
740-
# The patch is defined in data coordinates and when changing the
741-
# selector with square modifier and not in data coordinates, we need
742-
# to correct for the aspect ratio difference between the data and
743-
# display coordinate systems. Its value is typically provide by
744-
# Axes._get_aspect_ratio()
745-
self._aspect_ratio_correction = 1.0
746739
self._convert_units() # Validate the inputs.
747740

748741
def get_path(self):
@@ -770,13 +763,11 @@ def get_patch_transform(self):
770763
rotation_point = bbox.x0, bbox.y0
771764
else:
772765
rotation_point = self.rotation_point
773-
return transforms.BboxTransformTo(bbox) \
774-
+ transforms.Affine2D() \
775-
.translate(-rotation_point[0], -rotation_point[1]) \
776-
.scale(1, self._aspect_ratio_correction) \
777-
.rotate_deg(self.angle) \
778-
.scale(1, 1 / self._aspect_ratio_correction) \
779-
.translate(*rotation_point)
766+
return (transforms.BboxTransformTo(bbox) +
767+
transforms.Affine2D()
768+
.translate(-rotation_point[0], -rotation_point[1])
769+
.rotate_deg(self.angle)
770+
.translate(*rotation_point))
780771

781772
@property
782773
def rotation_point(self):
@@ -806,6 +797,24 @@ def get_xy(self):
806797
"""Return the left and bottom coords of the rectangle as a tuple."""
807798
return self._x0, self._y0
808799

800+
def get_edge_midpoints(self):
801+
"""
802+
Return the edge midpoints of the rectangle, moving clockwise from the
803+
center of the left-hand edge.
804+
"""
805+
return self.get_patch_transform().transform(
806+
[(0, 0.5), (0.5, 1), (1, 0.5), (0.5, 0)])
807+
808+
def get_corners(self):
809+
"""
810+
Return the corners of the rectangle, moving clockwise from (x, y).
811+
"""
812+
return self.get_patch_transform().transform(
813+
[(0, 0), (1, 0), (1, 1), (0, 1)])
814+
815+
def get_center(self):
816+
return self.get_patch_transform().transform((0.5, 0.5))
817+
809818
def get_width(self):
810819
"""Return the width of the rectangle."""
811820
return self._width
@@ -1551,12 +1560,6 @@ def __init__(self, xy, width, height, angle=0, **kwargs):
15511560
self._width, self._height = width, height
15521561
self._angle = angle
15531562
self._path = Path.unit_circle()
1554-
# Required for EllipseSelector with axes aspect ratio != 1
1555-
# The patch is defined in data coordinates and when changing the
1556-
# selector with square modifier and not in data coordinates, we need
1557-
# to correct for the aspect ratio difference between the data and
1558-
# display coordinate systems.
1559-
self._aspect_ratio_correction = 1.0
15601563
# Note: This cannot be calculated until this is added to an Axes
15611564
self._patch_transform = transforms.IdentityTransform()
15621565

@@ -1574,9 +1577,8 @@ def _recompute_transform(self):
15741577
width = self.convert_xunits(self._width)
15751578
height = self.convert_yunits(self._height)
15761579
self._patch_transform = transforms.Affine2D() \
1577-
.scale(width * 0.5, height * 0.5 * self._aspect_ratio_correction) \
1580+
.scale(width * 0.5, height * 0.5) \
15781581
.rotate_deg(self.angle) \
1579-
.scale(1, 1 / self._aspect_ratio_correction) \
15801582
.translate(*center)
15811583

15821584
def get_path(self):
@@ -1657,6 +1659,20 @@ def get_angle(self):
16571659

16581660
angle = property(get_angle, set_angle)
16591661

1662+
def get_corners(self):
1663+
"""
1664+
Return the corners of the rectangle, moving clockwise from (x0, y0).
1665+
"""
1666+
return self.get_patch_transform().transform(
1667+
[(-1, -1), (1, -1), (1, 1), (-1, 1)])
1668+
1669+
def get_edge_midpoints(self):
1670+
"""
1671+
Return the corners of the rectangle, moving clockwise from (x, y).
1672+
"""
1673+
return self.get_patch_transform().transform(
1674+
[(0, -1), (1, 0), (0, 1), (-1, 0)])
1675+
16601676

16611677
class Annulus(Patch):
16621678
"""

0 commit comments

Comments
 (0)