From c7f2f42145572e8e708fd2f01ecf4679e6534c54 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Tue, 22 Jun 2021 12:24:42 +0200 Subject: [PATCH] Backport PR #20480: Fix str of empty polygon. --- lib/matplotlib/patches.py | 7 +++++-- lib/matplotlib/tests/test_patches.py | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index a5bb2019db56..07ee830be31d 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -1048,8 +1048,11 @@ class Polygon(Patch): """A general polygon patch.""" def __str__(self): - s = "Polygon%d((%g, %g) ...)" - return s % (len(self._path.vertices), *tuple(self._path.vertices[0])) + if len(self._path.vertices): + s = "Polygon%d((%g, %g) ...)" + return s % (len(self._path.vertices), *self._path.vertices[0]) + else: + return "Polygon0()" @docstring.dedent_interpd def __init__(self, xy, closed=True, **kwargs): diff --git a/lib/matplotlib/tests/test_patches.py b/lib/matplotlib/tests/test_patches.py index 2a908098364e..4eacbac89126 100644 --- a/lib/matplotlib/tests/test_patches.py +++ b/lib/matplotlib/tests/test_patches.py @@ -347,6 +347,9 @@ def test_patch_str(): p = mpatches.PathPatch(path) assert str(p) == "PathPatch3((1, 2) ...)" + p = mpatches.Polygon(np.empty((0, 2))) + assert str(p) == "Polygon0()" + data = [[1, 2], [2, 2], [1, 2]] p = mpatches.Polygon(data) assert str(p) == "Polygon3((1, 2) ...)"