Skip to content

Backport PR #17982 on branch v3.3.x (BF: for degenerate polygons, add CLOSEPOLY vertex) #18009

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,13 +1063,27 @@ def set_xy(self, xy):
----------
xy : (N, 2) array-like
The coordinates of the vertices.

Notes
-----
Unlike `~.path.Path`, we do not ignore the last input vertex. If the
polygon is meant to be closed, and the last point of the polygon is not
equal to the first, we assume that the user has not explicitly passed a
``CLOSEPOLY`` vertex, and add it ourselves.
"""
xy = np.asarray(xy)
nverts, _ = xy.shape
if self._closed:
if len(xy) and (xy[0] != xy[-1]).any():
# if the first and last vertex are the "same", then we assume that
# the user explicitly passed the CLOSEPOLY vertex. Otherwise, we
# have to append one since the last vertex will be "ignored" by
# Path
if nverts == 1 or nverts > 1 and (xy[0] != xy[-1]).any():
xy = np.concatenate([xy, [xy[0]]])
else:
if len(xy) > 2 and (xy[0] == xy[-1]).all():
# if we aren't closed, and the last vertex matches the first, then
# we assume we have an unecessary CLOSEPOLY vertex and remove it
if nverts > 2 and (xy[0] == xy[-1]).all():
xy = xy[:-1]
self._path = Path(xy, closed=self._closed)
self.stale = True
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from matplotlib.patches import Polygon, Rectangle, FancyArrowPatch
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from matplotlib.transforms import Bbox
import matplotlib.pyplot as plt
from matplotlib import (
collections as mcollections, colors as mcolors, patches as mpatches,
Expand Down Expand Up @@ -556,3 +557,9 @@ def test_rotated_arcs():
ax.axvline(0, color="k")
ax.set_axis_off()
ax.set_aspect("equal")


def test_degenerate_polygon():
point = [0, 0]
correct_extents = Bbox([point, point]).extents
assert np.all(Polygon([point]).get_extents().extents == correct_extents)