From 6a25122704740218099330d197d099e48ca63d4f Mon Sep 17 00:00:00 2001 From: Karan Date: Sat, 1 Oct 2022 15:34:33 +0530 Subject: [PATCH 1/5] verifying that the inputs are of a consistent length in _parse_legend_args --- lib/matplotlib/legend.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 20d4a76e0db3..51636481d97e 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1260,6 +1260,10 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs): elif len(args) >= 2: 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.') From f61fed4de2ceecc946a67697c871e82d7699b0ef Mon Sep 17 00:00:00 2001 From: Karan Date: Sat, 1 Oct 2022 15:49:27 +0530 Subject: [PATCH 2/5] verify consistency of length of handles and labels --- lib/matplotlib/legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 51636481d97e..24c4c15234d6 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1261,7 +1261,7 @@ 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 + # Check length of handles and labels is consistent elif len(handles) != len(labels): raise ValueError("Number of handles not equal to number of labels.") From 9719a9ae8214682c9f9bbb89d958db6568fa5586 Mon Sep 17 00:00:00 2001 From: Karan Date: Sat, 1 Oct 2022 15:59:43 +0530 Subject: [PATCH 3/5] verify length consistancy --- lib/matplotlib/legend.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 24c4c15234d6..75ca5792e6d5 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1260,11 +1260,10 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs): elif len(args) >= 2: 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.') From c628028380f889f42fb8c82d375c8dacf994b47f Mon Sep 17 00:00:00 2001 From: Karan Date: Sat, 1 Oct 2022 20:03:55 +0530 Subject: [PATCH 4/5] add test to check for raised exception. --- lib/matplotlib/legend.py | 3 ++- lib/matplotlib/tests/test_legend.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 75ca5792e6d5..f62a2371b1a9 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1263,7 +1263,8 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs): # Check length of handles and labels is consistent elif len(handles) != len(labels): - raise ValueError("Number of handles not equal to number of 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..97bdaf1a5967 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']) From 1397a86c0c676b4f8ca70dfb1a410f5e58170c88 Mon Sep 17 00:00:00 2001 From: Karan Date: Sat, 1 Oct 2022 20:06:21 +0530 Subject: [PATCH 5/5] test for error raised --- lib/matplotlib/legend.py | 2 +- lib/matplotlib/tests/test_legend.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index f62a2371b1a9..fb18bb34fb8f 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1264,7 +1264,7 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs): # Check length of handles and labels is consistent elif len(handles) != len(labels): raise ValueError("Number of handles not equal " - "to number of labels.") + "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 97bdaf1a5967..5f67450dfd6e 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -976,5 +976,5 @@ 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."): + "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