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) ...)"