From 9897576ae5f5e49119cfb05b80a5e41cdc762907 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Tue, 19 Sep 2023 09:43:42 -0500 Subject: [PATCH] Backport PR #26825: Fix issue with non-string labels and legend --- lib/matplotlib/container.py | 2 +- lib/matplotlib/tests/test_container.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/container.py b/lib/matplotlib/container.py index e11fea391871..0f082e298afc 100644 --- a/lib/matplotlib/container.py +++ b/lib/matplotlib/container.py @@ -19,7 +19,7 @@ def __new__(cls, *args, **kwargs): def __init__(self, kl, label=None): self._callbacks = cbook.CallbackRegistry(signals=["pchanged"]) self._remove_method = None - self._label = label + self._label = str(label) if label is not None else None def remove(self): for c in cbook.flatten( diff --git a/lib/matplotlib/tests/test_container.py b/lib/matplotlib/tests/test_container.py index 8e894d9e9084..1e4577c518ae 100644 --- a/lib/matplotlib/tests/test_container.py +++ b/lib/matplotlib/tests/test_container.py @@ -1,3 +1,4 @@ +import numpy as np import matplotlib.pyplot as plt @@ -28,3 +29,9 @@ def test_errorbar_remove(): eb = ax.errorbar([1], [1], fmt='none') eb.remove() + + +def test_nonstring_label(): + # Test for #26824 + plt.bar(np.arange(10), np.random.rand(10), label=1) + plt.legend()