8000 Merge pull request #11212 from ImportanceOfBeingErnest/fix-circlepoly… · matplotlib/matplotlib@d045031 · GitHub
[go: up one dir, main page]

Skip to content

Commit d045031

Browse files
authored
Merge pull request #11212 from ImportanceOfBeingErnest/fix-circlepolygon-str
Fix CirclePolygon __str__ + adding tests
2 parents 6286e63 + 4404881 commit d045031

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

lib/matplotlib/patches.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,9 @@ class RegularPolygon(Patch):
794794
A regular polygon patch.
795795
"""
796796
def __str__(self):
797-
return "Poly%d(%g,%g)" % (self._numVertices, self._xy[0], self._xy[1])
797+
s = "RegularPolygon((%g, %g), %d, radius=%g, orientation=%g)"
798+
return s % (self._xy[0], self._xy[1], self._numVertices, self._radius,
799+
self._orientation)
798800

799801
@docstring.dedent_interpd
800802
def __init__(self, xy, numVertices, radius=5, orientation=0,
@@ -880,7 +882,8 @@ class PathPatch(Patch):
880882
_edge_default = True
881883

882884
def __str__(self):
883-
return "Poly((%g, %g) ...)" % tuple(self._path.vertices[0])
885+
s = "PathPatch%d((%g, %g) ...)"
886+
return s % (len(self._path.vertices), *tuple(self._path.vertices[0]))
884887

885888
@docstring.dedent_interpd
886889
def __init__(self, path, **kwargs):
@@ -908,7 +911,8 @@ class Polygon(Patch):
908911
A general polygon patch.
909912
"""
910913
def __str__(self):
911-
return "Poly((%g, %g) ...)" % tuple(self._path.vertices[0])
914+
s = "Polygon%d((%g, %g) ...)"
915+
return s % (len(self._path.vertices), *tuple(self._path.vertices[0]))
912916

913917
@docstring.dedent_interpd
914918
def __init__(self, xy, closed=True, **kwargs):
@@ -1381,7 +1385,8 @@ class CirclePolygon(RegularPolygon):
13811385
A polygon-approximation of a circle patch.
13821386
"""
13831387
def __str__(self):
1384-
return "CirclePolygon(%d,%d)" % self.center
1388+
s = "CirclePolygon((%g, %g), radius=%g, resolution=%d)"
1389+
return s % (self._xy[0], self._xy[1], self._radius, self._numVertices)
13851390

13861391
@docstring.dedent_interpd
13871392
def __init__(self, xy, radius=5,
@@ -2451,9 +2456,8 @@ class FancyBboxPatch(Patch):
24512456
_edge_default = True
24522457

24532458
def __str__(self):
2454-
return self.__class__.__name__ \
2455-
+ "(%g,%g;%gx%g)" % (self._x, self._y,
2456-
self._width, self._height)
2459+
s = self.__class__.__name__ + "((%g, %g), width=%g, height=%g)"
2460+
return s % (self._x, self._y, self._width, self._height)
24572461

24582462
@docstring.dedent_interpd
24592463
def __init__(self, xy, width, height,
@@ -3966,7 +3970,7 @@ def __str__(self):
39663970
if self._posA_posB is not None:
39673971
(x1, y1), (x2, y2) = self._posA_posB
39683972
return self.__class__.__name__ \
3969-
+ "(%g,%g->%g,%g)" % (x1, y1, x2, y2)
3973+
+ "((%g, %g)->(%g, %g))" % (x1, y1, x2, y2)
39703974
else:
39713975
return self.__class__.__name__ \
39723976
+ "(%s)" % (str(self._path_original),)
@@ -4374,7 +4378,7 @@ class ConnectionPatch(FancyArrowPatch):
43744378
connecting lines between two points (possibly in different axes).
43754379
"""
43764380
def __str__(self):
4377-
return "ConnectionPatch((%g,%g),(%g,%g))" % \
4381+
return "ConnectionPatch((%g, %g), (%g, %g))" % \
43784382
(self.xy1[0], self.xy1[1], self.xy2[0], self.xy2[1])
43794383

43804384
@docstring.dedent_interpd

lib/matplotlib/tests/test_patches.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,43 @@ def test_patch_str():
318318
expected = 'Arc(xy=(1, 2), width=3, height=4, angle=5, theta1=6, theta2=7)'
319319
assert str(p) == expected
320320

321+
p = mpatches.RegularPolygon((1, 2), 20, radius=5)
322+
assert str(p) == "RegularPolygon((1, 2), 20, radius=5, orientation=0)"
323+
324+
p = mpatches.CirclePolygon(xy=(1, 2), radius=5, resolution=20)
325+
assert str(p) == "CirclePolygon((1, 2), radius=5, resolution=20)"
326+
327+
p = mpatches.FancyBboxPatch((1, 2), width=3, height=4)
328+
assert str(p) == "FancyBboxPatch((1, 2), width=3, height=4)"
329+
330+
# Further nice __str__ which cannot be `eval`uated:
331+
path_data = [([1, 2], mpath.Path.MOVETO), ([2, 2], mpath.Path.LINETO),
332+
([1, 2], mpath.Path.CLOSEPOLY)]
333+
p = mpatches.PathPatch(mpath.Path(*zip(*path_data)))
334+
assert str(p) == "PathPatch3((1, 2) ...)"
335+
336+
data = [[1, 2], [2, 2], [1, 2]]
337+
p = mpatches.Polygon(data)
338+
assert str(p) == "Polygon3((1, 2) ...)"
339+
340+
p = mpatches.FancyArrowPatch(path=mpath.Path(*zip(*path_data)))
341+
assert str(p)[:27] == "FancyArrowPatch(Path(array("
342+
343+
p = mpatches.FancyArrowPatch((1, 2), (3, 4))
344+
assert str(p) == "FancyArrowPatch((1, 2)->(3, 4))"
345+
346+
p = mpatches.ConnectionPatch((1, 2), (3, 4), 'data')
347+
assert str(p) == "ConnectionPatch((1, 2), (3, 4))"
348+
349+
s = mpatches.Shadow(p, 1, 1)
350+
assert str(s) == "Shadow(ConnectionPatch((1, 2), (3, 4)))"
351+
352+
p = mpatches.YAArrow(plt.gcf(), (1, 0), (2, 1), width=0.1)
353+
assert str(p) == "YAArrow()"
354+
355+
# Not testing Arrow, FancyArrow here
356+
# because they seem to exist only for historical reasons.
357+
321358

322359
@image_comparison(baseline_images=['multi_color_hatch'],
323360
remove_text=True, style='default')

0 commit comments

Comments
 (0)
0