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

Skip to content

Commit 83a3716

Browse files
authored
Merge pull request #7536 from cdeil/issue-7536
Rectangle patch angle attribute and patch __str__ improvements
2 parents bf5aeb5 + 02bde21 commit 83a3716

2 files changed

Lines changed: 46 additions & 13 deletions

File tree

lib/matplotlib/patches.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,13 @@ def draw(self, renderer):
652652
class Rectangle(Patch):
653653
"""
654654
Draw a rectangle with lower left at *xy* = (*x*, *y*) with
655-
specified *width* and *height*.
655+
specified *width*, *height* and rotation *angle*.
656656
"""
657657

658658
def __str__(self):
659-
return self.__class__.__name__ \
660-
+ "(%g,%g;%gx%g)" % (self._x, self._y, self._width, self._height)
659+
pars = self._x, self._y, self._width, self._height, self.angle
660+
fmt = "Rectangle(xy=(%g, %g), width=%g, height=%g, angle=%g)"
661+
return fmt % pars
661662

662663
@docstring.dedent_interpd
663664
def __init__(self, xy, width, height, angle=0.0, **kwargs):
@@ -678,7 +679,7 @@ def __init__(self, xy, width, height, angle=0.0, **kwargs):
678679
self._y = float(xy[1])
679680
self._width = float(width)
680681
self._height = float(height)
681-
self._angle = float(angle)
682+
self.angle = float(angle)
682683
# Note: This cannot be calculated until this is added to an Axes
683684
self._rect_transform = transforms.IdentityTransform()
684685

@@ -700,7 +701,7 @@ def _update_patch_transform(self):
700701
height = self.convert_yunits(self._height)
701702
bbox = transforms.Bbox.from_bounds(x, y, width, height)
702703
rot_trans = transforms.Affine2D()
703-
rot_trans.rotate_deg_around(x, y, self._angle)
704+
rot_trans.rotate_deg_around(x, y, self.angle)
704705
self._rect_transform = transforms.BboxTransformTo(bbox)
705706
self._rect_transform += rot_trans
706707

@@ -1024,7 +1025,10 @@ class Wedge(Patch):
10241025
Wedge shaped patch.
10251026
"""
10261027
def __str__(self):
1027-
return "Wedge(%g,%g)" % (self.theta1, self.theta2)
1028+
pars = (self.center[0], self.center[1], self.r,
1029+
self.theta1, self.theta2, self.width)
1030+
fmt = "Wedge(center=(%g, %g), r=%g, theta1=%g, theta2=%g, width=%s)"
1031+
return fmt % pars
10281032

10291033
@docstring.dedent_interpd
10301034
def __init__(self, center, r, theta1, theta2, width=None, **kwargs):
@@ -1394,8 +1398,10 @@ class Ellipse(Patch):
13941398
A scale-free ellipse.
13951399
"""
13961400
def __str__(self):
1397-
return "Ellipse(%s,%s;%sx%s)" % (self.center[0], self.center[1],
1398-
self.width, self.height)
1401+
pars = (self.center[0], self.center[1],
1402+
self.width, self.height, self.angle)
1403+
fmt = "Ellipse(xy=(%s, %s), width=%s, height=%s, angle=%s)"
1404+
return fmt % pars
13991405

14001406
@docstring.dedent_interpd
14011407
def __init__(self, xy, width, height, angle=0.0, **kwargs):
@@ -1455,9 +1461,9 @@ class Circle(Ellipse):
14551461
A circle patch.
14561462
"""
14571463
def __str__(self):
1458-
return "Circle((%g,%g),r=%g)" % (self.center[0],
1459-
self.center[1],
1460-
self.radius)
1464+
pars = self.center[0], self.center[1], self.radius
1465+
fmt = "Circle(xy=(%g, %g), radius=%g)"
1466+
return fmt % pars
14611467

14621468
@docstring.dedent_interpd
14631469
def __init__(self, xy, radius=5, **kwargs):
@@ -1502,8 +1508,11 @@ class Arc(Ellipse):
15021508
with high resolution.
15031509
"""
15041510
def __str__(self):
1505-
return "Arc(%s,%s;%sx%s)" % (self.center[0], self.center[1],
1506-
self.width, self.height)
1511+
pars = (self.center[0], self.center[1], self.width,
1512+
self.height, self.angle, self.theta1, self.theta2)
1513+
fmt = ("Arc(xy=(%g, %g), width=%g, "
1514+
"height=%g, angle=%g, theta1=%g, theta2=%g)")
1515+
return fmt % pars
15071516

15081517
@docstring.dedent_interpd
15091518
def __init__(self, xy, width, height, angle=0.0,

lib/matplotlib/tests/test_patches.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,30 @@ def test_wedge_range():
291291
ax.set_ylim([-2, 9])
292292

293293

294+
def test_patch_str():
295+
"""
296+
Check that patches have nice and working `str` representation.
297+
298+
Note that the logic is that `__str__` is defined such that:
299+
str(eval(str(p))) == str(p)
300+
"""
301+
p = mpatches.Circle(xy=(1, 2), radius=3)
302+
assert str(p) == 'Circle(xy=(1, 2), radius=3)'
303+
304+
p = mpatches.Ellipse(xy=(1, 2), width=3, height=4, angle=5)
305+
assert str(p) == 'Ellipse(xy=(1, 2), width=3, height=4, angle=5)'
306+
307+
p = mpatches.Rectangle(xy=(1, 2), width=3, height=4, angle=5)
308+
assert str(p) == 'Rectangle(xy=(1, 2), width=3, height=4, angle=5)'
309+
310+
p = mpatches.Wedge(center=(1, 2), r=3, theta1=4, theta2=5, width=6)
311+
assert str(p) == 'Wedge(center=(1, 2), r=3, theta1=4, theta2=5, width=6)'
312+
313+
p = mpatches.Arc(xy=(1, 2), width=3, height=4, angle=5, theta1=6, theta2=7)
314+
expected = 'Arc(xy=(1, 2), width=3, height=4, angle=5, theta1=6, theta2=7)'
315+
assert str(p) == expected
316+
317+
294318
if __name__ == '__main__':
295319
import nose
296320
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)