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

Skip to content

Commit 3df5db5

Browse files
authored
Merge pull request #19646 from dstansby/rect-rotate
Add angle setter/getter to Rectangle
2 parents bd7df78 + 35d1e83 commit 3df5db5

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/matplotlib/patches.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,10 @@ def get_height(self):
799799
"""Return the height of the rectangle."""
800800
return self._height
801801

802+
def get_angle(self):
803+
"""Get the rotation angle in degrees."""
804+
return self.angle
805+
802806
def set_x(self, x):
803807
"""Set the left coordinate of the rectangle."""
804808
self._x0 = x
@@ -809,6 +813,15 @@ def set_y(self, y):
809813
self._y0 = y
810814
self.stale = True
811815

816+
def set_angle(self, angle):
817+
"""
818+
Set the rotation angle in degrees.
819+
820+
The rotation is performed anti-clockwise around *xy*.
821+
"""
822+
self.angle = angle
823+
self.stale = True
824+
812825
def set_xy(self, xy):
813826
"""
814827
Set the left and bottom coordinates of the rectangle.

lib/matplotlib/tests/test_patches.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ def test_rotate_rect():
7676
assert_almost_equal(rect1.get_verts(), new_verts)
7777

7878

79+
@check_figures_equal(extensions=['png'])
80+
def test_rotate_rect_draw(fig_test, fig_ref):
81+
ax_test = fig_test.add_subplot()
82+
ax_ref = fig_ref.add_subplot()
83+
84+
loc = (0, 0)
85+
width, height = (1, 1)
86+
angle = 30
87+
rect_ref = Rectangle(loc, width, height, angle=angle)
88+
ax_ref.add_patch(rect_ref)
89+
assert rect_ref.get_angle() == angle
90+
91+
# Check that when the angle is updated after adding to an axes, that the
92+
# patch is marked stale and redrawn in the correct location
93+
rect_test = Rectangle(loc, width, height)
94+
assert rect_test.get_angle() == 0
95+
ax_test.add_patch(rect_test)
96+
rect_test.set_angle(angle)
97+
assert rect_test.get_angle() == angle
98+
99+
79100
def test_negative_rect():
80101
# These two rectangles have the same vertices, but starting from a
81102
# different point. (We also drop the last vertex, which is a duplicate.)

0 commit comments

Comments
 (0)