Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


BUG: check for closed path in Polygon.set_xy() #1018

wants to merge 2 commits into from

4 participants


This was something unexpected I came across: I had to dig through the code to see why it didn't behave as I thought it should. I'm not sure whether it should be changed (I can see arguments both ways) but I thought I'd bring it up just in case.

Former behavior:

>>> xy = [[0,0], [0,1], [1,1]]
>>> p = Polygon(xy, closed=True)
>>> p.get_xy()
array([[ 0.,  0.],
       [ 0.,  1.],
       [ 1.,  1.],
       [ 0.,  0.]])
>>> p.set_xy(xy)
>>> p.get_xy()
array([[ 0.,  0.],
       [ 0.,  1.],
       [ 1.,  1.]])

This PR changes things so the second output is the same as the first, for both closed = True and closed = False.


I'm happy that this is like-for-like behaviour with the added set_xy fix. Because there is a behavioural change (or bug fix), probably needs an entry in the doc/api/api_changes.rst doc.

Other than that, gets my +1.


I think the idea is valid, but additional refactoring is needed. The logic in set_closed starting with "if closed:" should be moved into set_xy(), so that the closing point is not duplicated unnecessarily. set_closed would then end with "self.set_xy(self.get_xy)"


A agree with @efiring, but other than that, looks ok. Let's add a unit test, too.

@efiring efiring referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@efiring efiring referenced this pull request from a commit in efiring/matplotlib
@efiring efiring patches.polygon: fix bug in handling of path closing, #1018.
This is a modification of that pull request, with a test added.

This is superceded by #1071, so I am closing it.

@efiring efiring closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +2 −2 lib/matplotlib/
4 lib/matplotlib/
@@ -770,8 +770,6 @@ def __init__(self, xy, closed=True, **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]]])
def get_path(self):
@@ -796,6 +794,8 @@ def set_closed(self, closed):
def get_xy(self):
return self._path.vertices
def set_xy(self, vertices):
+ if self._closed and len(vertices):
+ vertices = np.concatenate([vertices, [vertices[0]]])
self._path = Path(vertices, closed=self._closed)
_get_xy = get_xy
_set_xy = set_xy
Something went wrong with that request. Please try again.