Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2251bd9

Browse files
committed
Add some helper methods to Patches
1 parent 98115e9 commit 2251bd9

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

lib/matplotlib/patches.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,25 @@ def get_xy(self):
806806
"""Return the left and bottom coords of the rectangle as a tuple."""
807807
return self._x0, self._y0
808808

809+
def get_edge_midpoints(self):
810+
"""
811+
Return midpoints of the edges of the rectangle, moving clockwise from
812+
the center of the left-hand edge.
813+
"""
814+
return self.get_patch_transform().transform(
815+
[(0, 0.5), (0.5, 1), (1, 0.5), (0.5, 0)])
816+
817+
def get_corners(self):
818+
"""
819+
Return the corners of the rectangle, moving clockwise from (x0, y0).
820+
"""
821+
return self.get_patch_transform().transform(
822+
[(0, 0), (1, 0), (1, 1), (0, 1)])
823+
824+
def get_center(self):
825+
"""Return the centre of the rectangle."""
826+
return self.get_patch_transform().transform((0.5, 0.5))
827+
809828
def get_width(self):
810829
"""Return the width of the rectangle."""
811830
return self._width
@@ -1657,6 +1676,22 @@ def get_angle(self):
16571676

16581677
angle = property(get_angle, set_angle)
16591678

1679+
def get_corners(self):
1680+
"""
1681+
Return the corners of the ellipse bounding box, moving clockwise from
1682+
the lower left.
1683+
"""
1684+
return self.get_patch_transform().transform(
1685+
[(-1, -1), (1, -1), (1, 1), (-1, 1)])
1686+
1687+
def get_edge_midpoints(self):
1688+
"""
1689+
Return the edge midpoints of the ellipse bounding box, moving clockwise
1690+
from the left hand edge.
1691+
"""
1692+
return self.get_patch_transform().transform(
1693+
[(-1, 0), (0, 1), (1, 0), (0, -1)])
1694+
16601695

16611696
class Annulus(Patch):
16621697
"""

lib/matplotlib/tests/test_patches.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77

88
import matplotlib as mpl
9-
from matplotlib.patches import (Annulus, Patch, Polygon, Rectangle,
9+
from matplotlib.patches import (Annulus, Ellipse, Patch, Polygon, Rectangle,
1010
FancyArrowPatch)
1111
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1212
from matplotlib.transforms import Bbox
@@ -54,6 +54,28 @@ def test_Polygon_close():
5454
assert_array_equal(p.get_xy(), xyclosed)
5555

5656

57+
def test_corners_edges():
58+
loc = [-6, 3]
59+
width = 1
60+
height = 5
61+
62+
corners = ((-6, 3), (-5, 3), (-5, 8), (-6, 8))
63+
edges = ((-6, 5.5), (-5.5, 8), (-5, 5.5), (-5.5, 3))
64+
# A rotated rectangle
65+
rect = Rectangle(loc, width, height)
66+
assert_array_equal(rect.get_corners(), corners)
67+
assert_array_equal(rect.get_edge_midpoints(), edges)
68+
assert_array_equal(rect.get_center(), (-5.5, 5.5))
69+
70+
# Ellipse uses center as reference point
71+
loc = [loc[0] + width / 2,
72+
loc[1] + height / 2]
73+
print(loc)
74+
ellipse = Ellipse(loc, width, height)
75+
assert_array_equal(ellipse.get_corners(), corners)
76+
assert_array_equal(ellipse.get_edge_midpoints(), edges)
77+
78+
5779
def test_rotate_rect():
5880
loc = np.asarray([1.0, 2.0])
5981
width = 2

0 commit comments

Comments
 (0)