diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 20d4a76e0db3..fb18bb34fb8f 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1261,6 +1261,10 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs): handles, labels = args[:2] extra_args = args[2:] + # Check length of handles and labels is consistent + elif len(handles) != len(labels): + raise ValueError("Number of handles not equal " + "to number of labels.") else: raise TypeError('Invalid arguments to legend.') diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index c0c5f79d71d8..5f67450dfd6e 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -970,3 +970,11 @@ def test_ncol_ncols(fig_test, fig_ref): ncols = 3 fig_test.legend(strings, ncol=ncols) fig_ref.legend(strings, ncols=ncols) + + +def test_handles_labels_length(self): + l1 = plt.plot(range(10)) + l2 = plt.plot(range(11)) + with pytest.raises(ValueError, match="Number of handles not equal " + "to number of labels."): + plt.legend([l1, l2], ['l1', 'l2', 'l3']) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy