From 0d5c018fcbbfa376fead21935a72932ca411a264 Mon Sep 17 00:00:00 2001 From: Bruno Beltran Date: Mon, 20 Jul 2020 14:24:54 -0700 Subject: [PATCH] BF: for degenerate polygons, add CLOSEPOLY vertex --- lib/matplotlib/patches.py | 18 ++++++++++++++++-- lib/matplotlib/tests/test_patches.py | 7 +++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index f06f988b4790..b06455569dbb 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -1066,13 +1066,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 diff --git a/lib/matplotlib/tests/test_patches.py b/lib/matplotlib/tests/test_patches.py index 475300b7c2d4..3b9d1e0adb3f 100644 --- a/lib/matplotlib/tests/test_patches.py +++ b/lib/matplotlib/tests/test_patches.py @@ -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, @@ -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)