Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3e54fff

Browse filesBrowse files
committed
Simplify Rectangle and RegularPolygon.
In Rectangle: no need to keep track and update of `self._x1` and `self._x2` all the time, just when we compute the patch_transform (which doesn't need to be split out into its own function, or stored into an instance variable, given that it's never accessed via the private attribute name). In RegularPolygon: likewise, we can just compute the transform in get_patch_transform(), and nowhere else, so the other attributes can just be plain attributes instead of properties that update the transform all the time.
1 parent f9d2918 commit 3e54fff
Copy full SHA for 3e54fff

File tree

Expand file treeCollapse file tree

1 file changed

+23
-96
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+23
-96
lines changed

‎lib/matplotlib/patches.py

Copy file name to clipboardExpand all lines: lib/matplotlib/patches.py
+23-96Lines changed: 23 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -729,59 +729,35 @@ def __init__(self, xy, width, height, angle=0.0, **kwargs):
729729
**kwargs : `.Patch` properties
730730
%(Patch)s
731731
"""
732-
733732
super().__init__(**kwargs)
734-
735733
self._x0 = xy[0]
736734
self._y0 = xy[1]
737-
738735
self._width = width
739736
self._height = height
740-
741-
self._x1 = self._x0 + self._width
742-
self._y1 = self._y0 + self._height
743-
744737
self.angle = float(angle)
745-
# Note: This cannot be calculated until this is added to an Axes
746-
self._rect_transform = transforms.IdentityTransform()
738+
self._convert_units() # Validate the inputs.
747739

748740
def get_path(self):
749741
"""Return the vertices of the rectangle."""
750742
return Path.unit_rectangle()
751743

752-
def _update_patch_transform(self):
753-
"""
754-
Notes
755-
-----
756-
This cannot be called until after this has been added to an Axes,
757-
otherwise unit conversion will fail. This makes it very important to
758-
call the accessor method and not directly access the transformation
759-
member variable.
760-
"""
761-
x0, y0, x1, y1 = self._convert_units()
762-
bbox = transforms.Bbox.from_extents(x0, y0, x1, y1)
763-
rot_trans = transforms.Affine2D()
764-
rot_trans.rotate_deg_around(x0, y0, self.angle)
765-
self._rect_transform = transforms.BboxTransformTo(bbox)
766-
self._rect_transform += rot_trans
767-
768-
def _update_x1(self):
769-
self._x1 = self._x0 + self._width
770-
771-
def _update_y1(self):
772-
self._y1 = self._y0 + self._height
773-
774744
def _convert_units(self):
775745
"""Convert bounds of the rectangle."""
776746
x0 = self.convert_xunits(self._x0)
777747
y0 = self.convert_yunits(self._y0)
778-
x1 = self.convert_xunits(self._x1)
779-
y1 = self.convert_yunits(self._y1)
748+
x1 = self.convert_xunits(self._x0 + self._width)
749+
y1 = self.convert_yunits(self._y0 + self._height)
780750
return x0, y0, x1, y1
781751

782752
def get_patch_transform(self):
783-
self._update_patch_transform()
784-
return self._rect_transform
753+
# Note: This cannot be called until after this has been added to
754+
# an Axes, otherwise unit conversion will fail. This makes it very
755+
# important to call the accessor method and not directly access the
756+
# transformation member variable.
757+
bbox = self.get_bbox()
758+
return (transforms.BboxTransformTo(bbox)
759+
+ transforms.Affine2D().rotate_deg_around(
760+
bbox.x0, bbox.y0, self.angle))
785761

786762
def get_x(self):
787763
"""Return the left coordinate of the rectangle."""
@@ -806,13 +782,11 @@ def get_height(self):
806782
def set_x(self, x):
807783
"""Set the left coordinate of the rectangle."""
808784
self._x0 = x
809-
self._update_x1()
810785
self.stale = True
811786

812787
def set_y(self, y):
813788
"""Set the bottom coordinate of the rectangle."""
814789
self._y0 = y
815-
self._update_y1()
816790
self.stale = True
817791

818792
def set_xy(self, xy):
@@ -824,20 +798,16 @@ def set_xy(self, xy):
824798
xy : (float, float)
825799
"""
826800
self._x0, self._y0 = xy
827-
self._update_x1()
828-
self._update_y1()
829801
self.stale = True
830802

831803
def set_width(self, w):
832804
"""Set the width of the rectangle."""
833805
self._width = w
834-
self._update_x1()
835806
self.stale = True
836807

837808
def set_height(self, h):
838809
"""Set the height of the rectangle."""
839810
self._height = h
840-
self._update_y1()
841811
self.stale = True
842812

843813
def set_bounds(self, *args):
@@ -859,8 +829,6 @@ def set_bounds(self, *args):
859829
self._y0 = b
860830
self._width = w
861831
self._height = h
862-
self._update_x1()
863-
self._update_y1()
864832
self.stale = True
865833

866834
def get_bbox(self):
@@ -876,8 +844,8 @@ class RegularPolygon(Patch):
876844

877845
def __str__(self):
878846
s = "RegularPolygon((%g, %g), %d, radius=%g, orientation=%g)"
879-
return s % (self._xy[0], self._xy[1], self._numVertices, self._radius,
880-
self._orientation)
847+
return s % (self.xy[0], self.xy[1], self.numvertices, self.radius,
848+
self.orientation)
881849

882850
@docstring.dedent_interpd
883851
def __init__(self, xy, numVertices, radius=5, orientation=0,
@@ -902,63 +870,22 @@ def __init__(self, xy, numVertices, radius=5, orientation=0,
902870
903871
%(Patch)s
904872
"""
905-
self._xy = xy
906-
self._numVertices = numVertices
907-
self._orientation = orientation
908-
self._radius = radius
873+
self.xy = xy
874+
self.numvertices = numVertices
875+
self.orientation = orientation
876+
self.radius = radius
909877
self._path = Path.unit_regular_polygon(numVertices)
910-
self._poly_transform = transforms.Affine2D()
911-
self._update_transform()
912-
878+
self._patch_transform = transforms.Affine2D()
913879
super().__init__(**kwargs)
914880

915-
def _update_transform(self):
916-
self._poly_transform.clear() \
917-
.scale(self.radius) \
918-
.rotate(self.orientation) \
919-
.translate(*self.xy)
920-
921-
@property
922-
def xy(self):
923-
return self._xy
924-
925-
@xy.setter
926-
def xy(self, xy):
927-
self._xy = xy
928-
self._update_transform()
929-
930-
@property
931-
def orientation(self):
932-
return self._orientation
933-
934-
@orientation.setter
935-
def orientation(self, orientation):
936-
self._orientation = orientation
937-
self._update_transform()
938-
939-
@property
940-
def radius(self):
941-
return self._radius
942-
943-
@radius.setter
944-
def radius(self, radius):
945-
self._radius = radius
946-
self._update_transform()
947-
948-
@property
949-
def numvertices(self):
950-
return self._numVertices
951-
952-
@numvertices.setter
953-
def numvertices(self, numVertices):
954-
self._numVertices = numVertices
955-
956881
def get_path(self):
957882
return self._path
958883

959884
def get_patch_transform(self):
960-
self._update_transform()
961-
return self._poly_transform
885+
return self._patch_transform.clear() \
886+
.scale(self.radius) \
887+
.rotate(self.orientation) \
888+
.translate(*self.xy)
962889

963890

964891
class PathPatch(Patch):
@@ -1461,7 +1388,7 @@ class CirclePolygon(RegularPolygon):
14611388

14621389
def __str__(self):
14631390
s = "CirclePolygon((%g, %g), radius=%g, resolution=%d)"
1464-
return s % (self._xy[0], self._xy[1], self._radius, self._numVertices)
1391+
return s % (self.xy[0], self.xy[1], self.radius, self.numvertices)
14651392

14661393
@docstring.dedent_interpd
14671394
def __init__(self, xy, radius=5,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.