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

Skip to content

Commit f404c94

Browse files
committed
Add angle kwarg to patches.Rectangle
Allows rotation of a rectangle upon instantiation.
1 parent 222a358 commit f404c94

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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.translate(-x, -y).rotate_deg(self._angle).translate(x, y)
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()

0 commit comments

Comments
 (0)