Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c8392b

Browse files
authored
Merge pull request #18009 from meeseeksmachine/auto-backport-of-pr-17982-on-v3.3.x
Backport PR #17982 on branch v3.3.x (BF: for degenerate polygons, add CLOSEPOLY vertex)
2 parents ac6aa6e + 07847dd commit 5c8392b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/matplotlib/patches.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,13 +1063,27 @@ def set_xy(self, xy):
10631063
----------
10641064
xy : (N, 2) array-like
10651065
The coordinates of the vertices.
1066+
1067+
Notes
1068+
-----
1069+
Unlike `~.path.Path`, we do not ignore the last input vertex. If the
1070+
polygon is meant to be closed, and the last point of the polygon is not
1071+
equal to the first, we assume that the user has not explicitly passed a
1072+
``CLOSEPOLY`` vertex, and add it ourselves.
10661073
"""
10671074
xy = np.asarray(xy)
1075+
nverts, _ = xy.shape
10681076
if self._closed:
1069-
if len(xy) and (xy[0] != xy[-1]).any():
1077+
# if the first and last vertex are the "same", then we assume that
1078+
# the user explicitly passed the CLOSEPOLY vertex. Otherwise, we
1079+
# have to append one since the last vertex will be "ignored" by
1080+
# Path
1081+
if nverts == 1 or nverts > 1 and (xy[0] != xy[-1]).any():
10701082
xy = np.concatenate([xy, [xy[0]]])
10711083
else:
1072-
if len(xy) > 2 and (xy[0] == xy[-1]).all():
1084+
# if we aren't closed, and the last vertex matches the first, then
1085+
# we assume we have an unecessary CLOSEPOLY vertex and remove it
1086+
if nverts > 2 and (xy[0] == xy[-1]).all():
10731087
xy = xy[:-1]
10741088
self._path = Path(xy, closed=self._closed)
10751089
self.stale = True

lib/matplotlib/tests/test_patches.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from matplotlib.patches import Polygon, Rectangle, FancyArrowPatch
99
from matplotlib.testing.decorators import image_comparison, check_figures_equal
10+
from matplotlib.transforms import Bbox
1011
import matplotlib.pyplot as plt
1112
from matplotlib import (
1213
collections as mcollections, colors as mcolors, patches as mpatches,
@@ -556,3 +557,9 @@ def test_rotated_arcs():
556557
ax.axvline(0, color="k")
557558
ax.set_axis_off()
558559
ax.set_aspect("equal")
560+
561+
562+
def test_degenerate_polygon():
563+
point = [0, 0]
564+
correct_extents = Bbox([point, point]).extents
565+
assert np.all(Polygon([point]).get_extents().extents == correct_extents)

0 commit comments

Comments
 (0)