Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patches.polygon: fix bug in handling of path closing, #1018. #1071

Merged
merged 3 commits into from Aug 12, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG
@@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/__init__.py
Expand Up @@ -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):
Expand Down
26 changes: 12 additions & 14 deletions lib/matplotlib/patches.py
Expand Up @@ -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
Expand All @@ -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(
Expand Down
42 changes: 42 additions & 0 deletions lib/matplotlib/tests/test_patches.py
@@ -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)