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

Skip to content

Commit 5a4d09d

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 bcc2f12 commit 5a4d09d

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
@@ -669,7 +669,7 @@ class Rectangle(Patch):
669669
"""
670670

671671
def __str__(self):
672-
pars = self._x, self._y, self._width, self._height, self.angle
672+
pars = self._x0, self._y0, self._width, self._height, self.angle
673673
fmt = "Rectangle(xy=(%g, %g), width=%g, height=%g, angle=%g)"
674674
return fmt % pars
675675

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

689689
Patch.__init__(self, **kwargs)
690690

691-
self._x = xy[0]
692-
self._y = xy[1]
691+
self._x0 = xy[0]
692+
self._y0 = xy[1]
693+
693694
self._width = width
694695
self._height = height
696+
697+
self._x1 = self._x0 + self._width
698+
self._y1 = self._y0 + self._height
699+
695700
self.angle = float(angle)
696701
# Note: This cannot be calculated until this is added to an Axes
697702
self._rect_transform = transforms.IdentityTransform()
@@ -708,31 +713,37 @@ def _update_patch_transform(self):
708713
maxes it very important to call the accessor method and
709714
not directly access the transformation member variable.
710715
"""
711-
x = self.convert_xunits(self._x)
712-
y = self.convert_yunits(self._y)
713-
width = self.convert_xunits(self._width)
714-
height = self.convert_yunits(self._height)
715-
bbox = transforms.Bbox.from_bounds(x, y, width, height)
716+
x0 = self.convert_xunits(self._x0)
717+
y0 = self.convert_yunits(self._y0)
718+
x1 = self.convert_xunits(self._x1)
719+
y1 = self.convert_yunits(self._y1)
720+
bbox = transforms.Bbox.from_extents(x0, y0, x1, y1)
716721
rot_trans = transforms.Affine2D()
717-
rot_trans.rotate_deg_around(x, y, self.angle)
722+
rot_trans.rotate_deg_around(x0, y0, self.angle)
718723
self._rect_transform = transforms.BboxTransformTo(bbox)
719724
self._rect_transform += rot_trans
720725

726+
def _update_x1(self):
727+
self._x1 = self._x0 + self._width
728+
729+
def _update_y1(self):
730+
self._y1 = self._y0 + self._height
731+
721732
def get_patch_transform(self):
722733
self._update_patch_transform()
723734
return self._rect_transform
724735

725736
def get_x(self):
726737
"Return the left coord of the rectangle"
727-
return self._x
738+
return self._x0
728739

729740
def get_y(self):
730741
"Return the bottom coord of the rectangle"
731-
return self._y
742+
return self._y0
732743

733744
def get_xy(self):
734745
"Return the left and bottom coords of the rectangle"
735-
return self._x, self._y
746+
return self._x0, self._y0
736747

737748
def get_width(self):
738749
"Return the width of the rectangle"
@@ -748,7 +759,8 @@ def set_x(self, x):
748759
749760
ACCEPTS: float
750761
"""
751-
self._x = x
762+
self._x0 = x
763+
self._update_x1()
752764
self.stale = True
753765

754766
def set_y(self, y):
@@ -757,7 +769,8 @@ def set_y(self, y):
757769
758770
ACCEPTS: float
759771
"""
760-
self._y = y
772+
self._y0 = y
773+
self._update_y1()
761774
self.stale = True
762775

763776
def set_xy(self, xy):
@@ -766,7 +779,9 @@ def set_xy(self, xy):
766779
767780
ACCEPTS: 2-item sequence
768781
"""
769-
self._x, self._y = xy
782+
self._x0, self._y0 = xy
783+
self._update_x1()
784+
self._update_y1()
770785
self.stale = True
771786

772787
def set_width(self, w):
@@ -776,6 +791,7 @@ def set_width(self, w):
776791
ACCEPTS: float
777792
"""
778793
self._width = w
794+
self._update_x1()
779795
self.stale = True
780796

781797
def set_height(self, h):
@@ -785,6 +801,7 @@ def set_height(self, h):
785801
ACCEPTS: float
786802
"""
787803
self._height = h
804+
self._update_y1()
788805
self.stale = True
789806

790807
def set_bounds(self, *args):
@@ -797,15 +814,17 @@ def set_bounds(self, *args):
797814
l, b, w, h = args[0]
798815
else:
799816
l, b, w, h = args
800-
self._x = l
801-
self._y = b
817+
self._x0 = l
818+
self._y0 = b
802819
self._width = w
803820
self._height = h
821+
self._update_x1()
822+
self._update_y1()
804823
self.stale = True
805824

806825
def get_bbox(self):
807-
return transforms.Bbox.from_bounds(self._x, self._y,
808-
self._width, self._height)
826+
return transforms.Bbox.from_extents(self._x0, self._y0,
827+
self._x1, self._y1)
809828

810829
xy = property(get_xy, set_xy)
811830

0 commit comments

Comments
 (0)