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

Skip to content

Add angle setter/getter to Rectangle #19646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,10 @@ def get_height(self):
"""Return the height of the rectangle."""
return self._height

def get_angle(self):
"""Get the rotation angle in degrees."""
return self.angle

def set_x(self, x):
"""Set the left coordinate of the rectangle."""
self._x0 = x
Expand All @@ -809,6 +813,15 @@ def set_y(self, y):
self._y0 = y
self.stale = True

def set_angle(self, angle):
"""
Set the rotation angle in degrees.

The rotation is performed anti-clockwise around *xy*.
"""
self.angle = angle
self.stale = True

def set_xy(self, xy):
"""
Set the left and bottom coordinates of the rectangle.
Expand Down
21 changes: 21 additions & 0 deletions lib/matplotlib/tests/test_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ def test_rotate_rect():
assert_almost_equal(rect1.get_verts(), new_verts)


@check_figures_equal(extensions=['png'])
def test_rotate_rect_draw(fig_test, fig_ref):
ax_test = fig_test.add_subplot()
ax_ref = fig_ref.add_subplot()

loc = (0, 0)
width, height = (1, 1)
angle = 30
rect_ref = Rectangle(loc, width, height, angle=angle)
ax_ref.add_patch(rect_ref)
assert rect_ref.get_angle() == angle

# Check that when the angle is updated after adding to an axes, that the
# patch is marked stale and redrawn in the correct location
rect_test = Rectangle(loc, width, height)
assert rect_test.get_angle() == 0
ax_test.add_patch(rect_test)
rect_test.set_angle(angle)
assert rect_test.get_angle() == angle


def test_negative_rect():
# These two rectangles have the same vertices, but starting from a
# different point. (We also drop the last vertex, which is a duplicate.)
Expand Down