diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index d01a8dca0847..2fb14e52c58c 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -459,6 +459,7 @@ def __init__( labels = [*reversed(labels)] handles = [*reversed(handles)] + handles = list(handles) if len(handles) < 2: ncols = 1 self._ncols = ncols if ncols != 1 else ncol diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 9c708598e27c..d80c6b2ed92a 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -42,6 +42,18 @@ def test_legend_ordereddict(): loc='center left', bbox_to_anchor=(1, .5)) +def test_legend_generator(): + # smoketest that generator inputs work + fig, ax = plt.subplots() + ax.plot([0, 1]) + ax.plot([0, 2]) + + handles = (line for line in ax.get_lines()) + labels = (label for label in ['spam', 'eggs']) + + ax.legend(handles, labels, loc='upper left') + + @image_comparison(['legend_auto1.png'], remove_text=True) def test_legend_auto1(): """Test automatic legend placement"""