Permalink
Browse files

Merge pull request #1071 from efiring/close_polygon

patches.polygon: fix bug in handling of path closing, #1018.
  • Loading branch information...
2 parents 447a7cc + a23cd4d commit 8a5afe3ee281ed25c724ba5408ebace128515950 @efiring efiring committed Aug 12, 2012
Showing with 60 additions and 14 deletions.
  1. +5 −0 CHANGELOG
  2. +1 −0 lib/matplotlib/__init__.py
  3. +12 −14 lib/matplotlib/patches.py
  4. +42 −0 lib/matplotlib/tests/test_patches.py
View
@@ -1,3 +1,8 @@
+2012-08-11 Fix path-closing bug in patches.Polygon, so that regardless
+ of whether the path is the initial one or was subsequently
+ set by set_xy(), get_xy() will return a closed path if and
+ only if get_closed() is True. Thanks to Jacob Vanderplas. - EF
+
2012-08-05 When a norm is passed to contourf, either or both of the
vmin, vmax attributes of that norm are now respected.
Formerly they were respected only if both were
@@ -1020,6 +1020,7 @@ def tk_window_focus():
'matplotlib.tests.test_delaunay',
'matplotlib.tests.test_legend',
'matplotlib.tests.test_colorbar',
+ 'matplotlib.tests.test_patches',
]
def test(verbosity=1):
View
@@ -767,12 +767,8 @@ def __init__(self, xy, closed=True, **kwargs):
"""
Patch.__init__(self, **kwargs)
- xy = np.asarray(xy, np.float_)
- self._path = Path(xy)
self._closed = closed
- if closed and len(xy):
- xy = np.concatenate([xy, [xy[0]]])
- self._set_xy(xy)
+ self.set_xy(xy)
def get_path(self):
return self._path
@@ -783,20 +779,22 @@ def get_closed(self):
def set_closed(self, closed):
if self._closed == bool(closed):
return
- self._closed = closed
- xy = self._get_xy()
- if closed:
+ self._closed = bool(closed)
+ self.set_xy(self.get_xy())
+
+ def get_xy(self):
+ return self._path.vertices
+
+ def set_xy(self, xy):
+ xy = np.asarray(xy)
+ if self._closed:
if len(xy) and (xy[0] != xy[-1]).any():
xy = np.concatenate([xy, [xy[0]]])
else:
if len(xy)>2 and (xy[0]==xy[-1]).all():
- xy = xy[0:-1]
- self._set_xy(xy)
+ xy = xy[:-1]
+ self._path = Path(xy, closed=self._closed)
- def get_xy(self):
- return self._path.vertices
- def set_xy(self, vertices):
- self._path = Path(vertices, closed=self._closed)
_get_xy = get_xy
_set_xy = set_xy
xy = property(
@@ -0,0 +1,42 @@
+"""
+Tests specific to the patches module.
+"""
+
+from numpy.testing import assert_array_equal
+from matplotlib.patches import Polygon
+
+def test_Polygon_close():
+ """
+ Github issue #1018 identified a bug in the Polygon handling
+ of the closed attribute; the path was not getting closed
+ when set_xy was used to set the vertices.
+ """
+ # open set of vertices:
+ xy = [[0,0], [0,1], [1,1]]
+ # closed set:
+ xyclosed = xy + [[0,0]]
+
+ # start with open path and close it:
+ p = Polygon(xy, closed=True)
+ assert_array_equal(p.get_xy(), xyclosed)
+ p.set_xy(xy)
+ assert_array_equal(p.get_xy(), xyclosed)
+
+ # start with closed path and open it:
+ p = Polygon(xyclosed, closed=False)
+ assert_array_equal(p.get_xy(), xy)
+ p.set_xy(xyclosed)
+ assert_array_equal(p.get_xy(), xy)
+
+ # start with open path and leave it open:
+ p = Polygon(xy, closed=False)
+ assert_array_equal(p.get_xy(), xy)
+ p.set_xy(xy)
+ assert_array_equal(p.get_xy(), xy)
+
+ # start with closed path and leave it closed:
+ p = Polygon(xyclosed, closed=True)
+ assert_array_equal(p.get_xy(), xyclosed)
+ p.set_xy(xyclosed)
+ assert_array_equal(p.get_xy(), xyclosed)
+

0 comments on commit 8a5afe3

Please sign in to comment.