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

Skip to content

Commit 17e1cf8

Browse files
committed
Merge pull request #1405 from dmcdougall/rect_angle_kwarg
Add angle kwarg to patches.Rectangle
2 parents fc17080 + 3a98ff0 commit 17e1cf8

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

doc/users/whats_new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ revision, see the :ref:`github-stats`.
2222
new in matplotlib-1.3
2323
=====================
2424

25+
Initialize a rotated rectangle
26+
------------------------------
27+
Damon McDougall extended the :class:`~matplotlib.patches.Rectangle` constructor
28+
to accept an `angle` kwarg, specifying the rotation of a rectangle in degrees.
29+
2530
Rectangular colorbar extensions
2631
-------------------------------
2732
Andrew Dawson added a new keyword argument *extendrect* to

lib/matplotlib/patches.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,12 @@ def __str__(self):
539539
+ "(%g,%g;%gx%g)" % (self._x, self._y, self._width, self._height)
540540

541541
@docstring.dedent_interpd
542-
def __init__(self, xy, width, height, **kwargs):
542+
def __init__(self, xy, width, height, angle=0.0, **kwargs):
543543
"""
544544
545+
*angle*
546+
rotation in degrees (anti-clockwise)
547+
545548
*fill* is a boolean indicating whether to fill the rectangle
546549
547550
Valid kwargs are:
@@ -554,6 +557,7 @@ def __init__(self, xy, width, height, **kwargs):
554557
self._y = xy[1]
555558
self._width = width
556559
self._height = height
560+
self._angle = angle
557561
# Note: This cannot be calculated until this is added to an Axes
558562
self._rect_transform = transforms.IdentityTransform()
559563

@@ -574,7 +578,10 @@ def _update_patch_transform(self):
574578
width = self.convert_xunits(self._width)
575579
height = self.convert_yunits(self._height)
576580
bbox = transforms.Bbox.from_bounds(x, y, width, height)
581+
rot_trans = transforms.Affine2D()
582+
rot_trans.rotate_deg_around(x, y, self._angle)
577583
self._rect_transform = transforms.BboxTransformTo(bbox)
584+
self._rect_transform += rot_trans
578585

579586
def get_patch_transform(self):
580587
self._update_patch_transform()

lib/matplotlib/tests/test_patches.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
Tests specific to the patches module.
33
"""
44

5+
import numpy as np
56
from numpy.testing import assert_array_equal
7+
from numpy.testing import assert_almost_equal
68
from matplotlib.patches import Polygon
9+
from matplotlib.patches import Rectangle
710

811
def test_Polygon_close():
912
"""
@@ -40,3 +43,25 @@ def test_Polygon_close():
4043
p.set_xy(xyclosed)
4144
assert_array_equal(p.get_xy(), xyclosed)
4245

46+
def test_rotate_rect():
47+
loc = np.asarray([1.0, 2.0])
48+
width = 2
49+
height = 3
50+
angle = 30.0
51+
52+
# A rotated rectangle
53+
rect1 = Rectangle(loc, width, height, angle=angle)
54+
55+
# A non-rotated rectangle
56+
rect2 = Rectangle(loc, width, height)
57+
58+
# Set up an explicit rotation matrix (in radians)
59+
angle_rad = np.pi * angle / 180.0
60+
rotation_matrix = np.array([[np.cos(angle_rad), -np.sin(angle_rad)],
61+
[np.sin(angle_rad), np.cos(angle_rad)]])
62+
63+
# Translate to origin, rotate each vertex, and then translate back
64+
new_verts = np.inner(rotation_matrix, rect2.get_verts() - loc).T + loc
65+
66+
# They should be the same
67+
assert_almost_equal(rect1.get_verts(), new_verts)

0 commit comments

Comments
 (0)