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

Skip to content

Commit e8ce736

Browse files
committed
Use rectangle coords instead of width/height
Fix up some incorrect bits in Rectangle Remember to update x1/y1 when setting x0/y0 Add methods to update x1/y1
1 parent 6336f2d commit e8ce736

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

lib/matplotlib/patches.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ class Rectangle(Patch):
643643
"""
644644

645645
def __str__(self):
646-
pars = self._x, self._y, self._width, self._height, self.angle
646+
pars = self._x0, self._y0, self._width, self._height, self.angle
647647
fmt = "Rectangle(xy=(%g, %g), width=%g, height=%g, angle=%g)"
648648
return fmt % pars
649649

@@ -662,10 +662,15 @@ def __init__(self, xy, width, height, angle=0.0, **kwargs):
662662

663663
Patch.__init__(self, **kwargs)
664664

665-
self._x = xy[0]
666-
self._y = xy[1]
665+
self._x0 = xy[0]
666+
self._y0 = xy[1]
667+
667668
self._width = width
668669
self._height = height
670+
671+
self._x1 = self._x0 + self._width
672+
self._y1 = self._y0 + self._height
673+
669674
self.angle = float(angle)
670675
# Note: This cannot be calculated until this is added to an Axes
671676
self._rect_transform = transforms.IdentityTransform()
@@ -682,31 +687,37 @@ def _update_patch_transform(self):
682687
makes it very important to call the accessor method and
683688
not directly access the transformation member variable.
684689
"""
685-
x = self.convert_xunits(self._x)
686-
y = self.convert_yunits(self._y)
687-
width = self.convert_xunits(self._width)
688-
height = self.convert_yunits(self._height)
689-
bbox = transforms.Bbox.from_bounds(x, y, width, height)
690+
x0 = self.convert_xunits(self._x0)
691+
y0 = self.convert_yunits(self._y0)
692+
x1 = self.convert_xunits(self._x1)
693+
y1 = self.convert_yunits(self._y1)
694+
bbox = transforms.Bbox.from_extents(x0, y0, x1, y1)
690695
rot_trans = transforms.Affine2D()
691-
rot_trans.rotate_deg_around(x, y, self.angle)
696+
rot_trans.rotate_deg_around(x0, y0, self.angle)
692697
self._rect_transform = transforms.BboxTransformTo(bbox)
693698
self._rect_transform += rot_trans
694699

700+
def _update_x1(self):
701+
self._x1 = self._x0 + self._width
702+
703+
def _update_y1(self):
704+
self._y1 = self._y0 + self._height
705+
695706
def get_patch_transform(self):
696707
self._update_patch_transform()
697708
return self._rect_transform
698709

699710
def get_x(self):
700711
"Return the left coord of the rectangle"
701-
return self._x
712+
return self._x0
702713

703714
def get_y(self):
704715
"Return the bottom coord of the rectangle"
705-
return self._y
716+
return self._y0
706717

707718
def get_xy(self):
708719
"Return the left and bottom coords of the rectangle"
709-
return self._x, self._y
720+
return self._x0, self._y0
710721

711722
def get_width(self):
712723
"Return the width of the rectangle"
@@ -722,7 +733,8 @@ def set_x(self, x):
722733
723734
ACCEPTS: float
724735
"""
725-
self._x = x
736+
self._x0 = x
737+
self._update_x1()
726738
self.stale = True
727739

728740
def set_y(self, y):
@@ -731,7 +743,8 @@ def set_y(self, y):
731743
732744
ACCEPTS: float
733745
"""
734-
self._y = y
746+
self._y0 = y
747+
self._update_y1()
735748
self.stale = True
736749

737750
def set_xy(self, xy):
@@ -740,7 +753,9 @@ def set_xy(self, xy):
740753
741754
ACCEPTS: 2-item sequence
742755
"""
743-
self._x, self._y = xy
756+
self._x0, self._y0 = xy
757+
self._update_x1()
758+
self._update_y1()
744759
self.stale = True
745760

746761
def set_width(self, w):
@@ -750,6 +765,7 @@ def set_width(self, w):
750765
ACCEPTS: float
751766
"""
752767
self._width = w
768+
self._update_x1()
753769
self.stale = True
754770

755771
def set_height(self, h):
@@ -759,6 +775,7 @@ def set_height(self, h):
759775
ACCEPTS: float
760776
"""
761777
self._height = h
778+
self._update_y1()
762779
self.stale = True
763780

764781
def set_bounds(self, *args):
@@ -771,15 +788,17 @@ def set_bounds(self, *args):
771788
l, b, w, h = args[0]
772789
else:
773790
l, b, w, h = args
774-
self._x = l
775-
self._y = b
791+
self._x0 = l
792+
self._y0 = b
776793
self._width = w
777794
self._height = h
795+
self._update_x1()
796+
self._update_y1()
778797
self.stale = True
779798

780799
def get_bbox(self):
781-
return transforms.Bbox.from_bounds(self._x, self._y,
782-
self._width, self._height)
800+
return transforms.Bbox.from_extents(self._x0, self._y0,
801+
self._x1, self._y1)
783802

784803
xy = property(get_xy, set_xy)
785804

0 commit comments

Comments
 (0)