From 5a26bb9511bcfa8c2fc375fda7233aa0f33dbf81 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:49:06 -0500 Subject: [PATCH 01/19] Raise warning if both c and facecolors are used in scatter plot. In the future we may implement c as edgecolors in that case or cause plot to fail. --- lib/matplotlib/axes/_axes.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index f6a4ebfdc7c6..5af7b1997465 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4613,10 +4613,20 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize, edgecolors = kwcolor if facecolors is None: facecolors = kwcolor - + + if edgecolors is None and not mpl.rcParams['_internal.classic_mode']: edgecolors = mpl.rcParams['scatter.edgecolors'] + # Raise a warning if both `c` and `facecolor` are set (issue #24404). + if c is not None and facecolors is not None: + _api.warn_external( + "You passed both c and facecolor/facecolors for the markers. " + "Matplotlib is ignoring the facecolor " + "in favor of what you specified in c. " + "This behavior may change in the future." + ) + c_was_none = c is None if c is None: c = (facecolors if facecolors is not None From f1966c2eb416f553d86d30cc5ac6d8598c2ac77b Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:26:16 -0500 Subject: [PATCH 02/19] Corrected flake8 formatting warnings. --- lib/matplotlib/axes/_axes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5af7b1997465..5ac0cba034ee 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4613,8 +4613,7 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize, edgecolors = kwcolor if facecolors is None: facecolors = kwcolor - - + if edgecolors is None and not mpl.rcParams['_internal.classic_mode']: edgecolors = mpl.rcParams['scatter.edgecolors'] From 53a082516f02b9d564a45d81d85b720d9b7b41b1 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Tue, 12 Nov 2024 20:38:42 -0500 Subject: [PATCH 03/19] Removed offending whitespace and extra line. To pass flake8 formatting check --- lib/matplotlib/axes/_axes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5af7b1997465..5ac0cba034ee 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4613,8 +4613,7 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize, edgecolors = kwcolor if facecolors is None: facecolors = kwcolor - - + if edgecolors is None and not mpl.rcParams['_internal.classic_mode']: edgecolors = mpl.rcParams['scatter.edgecolors'] From 80318093766a8822a983cffc71576f43d9f74312 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:11:58 -0500 Subject: [PATCH 04/19] Corrected old test that qualified for the new warning. --- lib/matplotlib/tests/test_legend.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 61892378bd03..feb5ef0226f4 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -974,7 +974,6 @@ def test_legend_pathcollection_labelcolor_markfacecolor_cmap(): np.arange(10), np.arange(10), label='#1', - c=np.arange(10), facecolor=facecolors ) From 8ae213cd15a9791b350fa9b1d9a0b68e90ed6d6d Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Mon, 18 Nov 2024 11:58:08 -0500 Subject: [PATCH 05/19] Made message more concise, and straightened old test, to fulfill @timhoffm's points. --- lib/matplotlib/axes/_axes.py | 3 +-- lib/matplotlib/tests/test_legend.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5ac0cba034ee..73c351719328 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4621,8 +4621,7 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize, if c is not None and facecolors is not None: _api.warn_external( "You passed both c and facecolor/facecolors for the markers. " - "Matplotlib is ignoring the facecolor " - "in favor of what you specified in c. " + "c has precedence over facecolor/facecolors. " "This behavior may change in the future." ) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index feb5ef0226f4..c8963422fdd5 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -969,12 +969,12 @@ def test_legend_pathcollection_labelcolor_markfacecolor_cmap(): # test the labelcolor for labelcolor='markerfacecolor' on PathCollection # with colormaps fig, ax = plt.subplots() - facecolors = mpl.cm.viridis(np.random.rand(10)) + colormap = mpl.cm.viridis(np.random.rand(10)) ax.scatter( np.arange(10), np.arange(10), label='#1', - facecolor=facecolors + c=colormap ) leg = ax.legend(labelcolor='markerfacecolor') From 49f03f723ebb18a6e498a80abdfc006a97ba840b Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:04:16 -0500 Subject: [PATCH 06/19] Added test. --- lib/matplotlib/tests/test_axes.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index e3a59a1751ab..9aa1c35b14d3 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2765,6 +2765,24 @@ def test_scatter_color_warning(self, kwargs): plt.scatter([], [], c=[], **kwargs) plt.scatter([1, 2], [3, 4], c=[4, 5], **kwargs) + @pytest.mark.parametrize('colors', + [ + ('red', 'blue'), # simple string colors + (['red', 'blue'], ['green', 'yellow']), # array of colors + ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) # RGB values + ]) + def test_scatter_c_facecolor_warning(self, colors): + warn_match = ( + "You passed both c and facecolor/facecolors for the markers. " + "c has precedence over facecolor/facecolors. This behavior may " + "change in the future." + ) + fig, ax = plt.subplots() + x = [0, 1] if len(colors[0]) == 2 else [0] + y = x + with pytest.warns(UserWarning, match=warn_match): + ax.scatter(x, y, c=colors[0], facecolors=colors[1]) + def test_scatter_unfilled(self): coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'], marker=mmarkers.MarkerStyle('o', fillstyle='none'), From 8234066c28c254e2bc043e6fb96c4ef5a6f1dc4f Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:12:44 -0500 Subject: [PATCH 07/19] Corrected flake8 linting objections. --- lib/matplotlib/tests/test_axes.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9aa1c35b14d3..b6cb81a70e3e 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2765,12 +2765,12 @@ def test_scatter_color_warning(self, kwargs): plt.scatter([], [], c=[], **kwargs) plt.scatter([1, 2], [3, 4], c=[4, 5], **kwargs) - @pytest.mark.parametrize('colors', - [ - ('red', 'blue'), # simple string colors - (['red', 'blue'], ['green', 'yellow']), # array of colors - ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) # RGB values - ]) + @pytest.mark.parametrize('colors', + [ + ('red', 'blue'), + (['red', 'blue'], ['green', 'yellow']), + ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) + ]) def test_scatter_c_facecolor_warning(self, colors): warn_match = ( "You passed both c and facecolor/facecolors for the markers. " @@ -2782,7 +2782,7 @@ def test_scatter_c_facecolor_warning(self, colors): y = x with pytest.warns(UserWarning, match=warn_match): ax.scatter(x, y, c=colors[0], facecolors=colors[1]) - + def test_scatter_unfilled(self): coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'], marker=mmarkers.MarkerStyle('o', fillstyle='none'), From 38c092a8faa029842484264b4b490f24a1a74723 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:12:17 -0500 Subject: [PATCH 08/19] Added direct test of _parse method to attempt to make Codecov happy. --- lib/matplotlib/tests/test_axes.py | 67 ++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index b6cb81a70e3e..d9637554ea18 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2765,24 +2765,6 @@ def test_scatter_color_warning(self, kwargs): plt.scatter([], [], c=[], **kwargs) plt.scatter([1, 2], [3, 4], c=[4, 5], **kwargs) - @pytest.mark.parametrize('colors', - [ - ('red', 'blue'), - (['red', 'blue'], ['green', 'yellow']), - ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) - ]) - def test_scatter_c_facecolor_warning(self, colors): - warn_match = ( - "You passed both c and facecolor/facecolors for the markers. " - "c has precedence over facecolor/facecolors. This behavior may " - "change in the future." - ) - fig, ax = plt.subplots() - x = [0, 1] if len(colors[0]) == 2 else [0] - y = x - with pytest.warns(UserWarning, match=warn_match): - ax.scatter(x, y, c=colors[0], facecolors=colors[1]) - def test_scatter_unfilled(self): coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'], marker=mmarkers.MarkerStyle('o', fillstyle='none'), @@ -3066,6 +3048,55 @@ def get_next_color(): c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color) +# Warning message tested in the next two tests. +WARN_MSG = ( + "You passed both c and facecolor/facecolors for the markers. " + "c has precedence over facecolor/facecolors. This behavior may " + "change in the future." +) +# Test cases shared between direct and integration tests +COLOR_TEST_CASES = [ + ('red', 'blue'), + (['red', 'blue'], ['green', 'yellow']), + ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) +] + + +@pytest.mark.parametrize('c, facecolor', COLOR_TEST_CASES) +def test_parse_c_facecolor_warning_direct(c, facecolor): + """Test the internal _parse_scatter_color_args method directly.""" + def get_next_color(): + return 'blue' + + # Test with facecolors (plural) + with pytest.warns(UserWarning, match=WARN_MSG): + mpl.axes.Axes._parse_scatter_color_args( + c=c, edgecolors=None, kwargs={'facecolors': facecolor}, + xsize=2, get_next_color_func=get_next_color) + + # Test with facecolor (singular) + with pytest.warns(UserWarning, match=WARN_MSG): + mpl.axes.Axes._parse_scatter_color_args( + c=c, edgecolors=None, kwargs={'facecolor': facecolor}, + xsize=2, get_next_color_func=get_next_color) + + +@pytest.mark.parametrize('c, facecolor', COLOR_TEST_CASES) +def test_scatter_c_facecolor_warning_integration(c, facecolor): + """Test the warning through the actual scatter plot creation.""" + fig, ax = plt.subplots() + x = [0, 1] if isinstance(c, (list, tuple)) else [0] + y = x + + # Test with facecolors (plural) + with pytest.warns(UserWarning, match=WARN_MSG): + ax.scatter(x, y, c=c, facecolors=facecolor) + + # Test with facecolor (singular) + with pytest.warns(UserWarning, match=WARN_MSG): + ax.scatter(x, y, c=c, facecolor=facecolor) + + def test_as_mpl_axes_api(): # tests the _as_mpl_axes api class Polar: From e6e49aa3d200da802c23297075cfc20dae2bb510 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:10:05 -0500 Subject: [PATCH 09/19] Added pragma tags for code coverage to ignore unused but untested simplified return. --- lib/matplotlib/tests/test_axes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index d9637554ea18..ae2192ae2715 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2906,7 +2906,7 @@ def test_scatter_different_shapes(self, fig_test, fig_ref): @pytest.mark.parametrize('c_case, re_key', params_test_scatter_c) def test_scatter_c(self, c_case, re_key): def get_next_color(): - return 'blue' # currently unused + return 'blue' # currently unused # pragma: no cover xsize = 4 # Additional checking of *c* (introduced in #11383). @@ -3000,7 +3000,7 @@ def _params(c=None, xsize=2, *, edgecolors=None, **kwargs): ]) def test_parse_scatter_color_args(params, expected_result): def get_next_color(): - return 'blue' # currently unused + return 'blue' # currently unused # pragma: no cover c, colors, _edgecolors = mpl.axes.Axes._parse_scatter_color_args( *params, get_next_color_func=get_next_color) @@ -3027,7 +3027,7 @@ def get_next_color(): ]) def test_parse_scatter_color_args_edgecolors(kwargs, expected_edgecolors): def get_next_color(): - return 'blue' # currently unused + return 'blue' # currently unused # pragma: no cover c = kwargs.pop('c', None) edgecolors = kwargs.pop('edgecolors', None) @@ -3039,7 +3039,7 @@ def get_next_color(): def test_parse_scatter_color_args_error(): def get_next_color(): - return 'blue' # currently unused + return 'blue' # currently unused # pragma: no cover with pytest.raises(ValueError, match="RGBA values should be within 0-1 range"): @@ -3066,7 +3066,7 @@ def get_next_color(): def test_parse_c_facecolor_warning_direct(c, facecolor): """Test the internal _parse_scatter_color_args method directly.""" def get_next_color(): - return 'blue' + return 'blue' # currently unused # pragma: no cover # Test with facecolors (plural) with pytest.warns(UserWarning, match=WARN_MSG): From 39e18997a2d680fb8f2e260f4e5325f166b3a0f5 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:49:06 -0500 Subject: [PATCH 10/19] Raise warning if both c and facecolors are used in scatter plot. In the future we may implement c as edgecolors in that case or cause plot to fail. --- lib/matplotlib/axes/_axes.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 9bfcc446f6a6..23510d4b2d15 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4666,10 +4666,20 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize, edgecolors = kwcolor if facecolors is None: facecolors = kwcolor - + + if edgecolors is None and not mpl.rcParams['_internal.classic_mode']: edgecolors = mpl.rcParams['scatter.edgecolors'] + # Raise a warning if both `c` and `facecolor` are set (issue #24404). + if c is not None and facecolors is not None: + _api.warn_external( + "You passed both c and facecolor/facecolors for the markers. " + "Matplotlib is ignoring the facecolor " + "in favor of what you specified in c. " + "This behavior may change in the future." + ) + c_was_none = c is None if c is None: c = (facecolors if facecolors is not None From 06c8a76eb27942c8f83bc401bc934a9a0eb783e1 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:26:16 -0500 Subject: [PATCH 11/19] Corrected flake8 formatting warnings. --- lib/matplotlib/axes/_axes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 23510d4b2d15..6fc458babb09 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4666,8 +4666,7 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize, edgecolors = kwcolor if facecolors is None: facecolors = kwcolor - - + if edgecolors is None and not mpl.rcParams['_internal.classic_mode']: edgecolors = mpl.rcParams['scatter.edgecolors'] From 72138c6ce0f45b053e54eea8ff89e2e0983c780a Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:11:58 -0500 Subject: [PATCH 12/19] Corrected old test that qualified for the new warning. --- lib/matplotlib/tests/test_legend.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 61892378bd03..feb5ef0226f4 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -974,7 +974,6 @@ def test_legend_pathcollection_labelcolor_markfacecolor_cmap(): np.arange(10), np.arange(10), label='#1', - c=np.arange(10), facecolor=facecolors ) From 319b92b002920f4a4440dc2cd06c32d59b470e80 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Mon, 18 Nov 2024 11:58:08 -0500 Subject: [PATCH 13/19] Made message more concise, and straightened old test, to fulfill @timhoffm's points. --- lib/matplotlib/axes/_axes.py | 3 +-- lib/matplotlib/tests/test_legend.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 6fc458babb09..72af2674c676 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4674,8 +4674,7 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize, if c is not None and facecolors is not None: _api.warn_external( "You passed both c and facecolor/facecolors for the markers. " - "Matplotlib is ignoring the facecolor " - "in favor of what you specified in c. " + "c has precedence over facecolor/facecolors. " "This behavior may change in the future." ) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index feb5ef0226f4..c8963422fdd5 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -969,12 +969,12 @@ def test_legend_pathcollection_labelcolor_markfacecolor_cmap(): # test the labelcolor for labelcolor='markerfacecolor' on PathCollection # with colormaps fig, ax = plt.subplots() - facecolors = mpl.cm.viridis(np.random.rand(10)) + colormap = mpl.cm.viridis(np.random.rand(10)) ax.scatter( np.arange(10), np.arange(10), label='#1', - facecolor=facecolors + c=colormap ) leg = ax.legend(labelcolor='markerfacecolor') From e348dfc52b43040cc704160f17022a667cab05a6 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:04:16 -0500 Subject: [PATCH 14/19] Added test. --- lib/matplotlib/tests/test_axes.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a237713cafe7..d2bc0f33ebb8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2779,6 +2779,24 @@ def test_scatter_color_warning(self, kwargs): plt.scatter([], [], c=[], **kwargs) plt.scatter([1, 2], [3, 4], c=[4, 5], **kwargs) + @pytest.mark.parametrize('colors', + [ + ('red', 'blue'), # simple string colors + (['red', 'blue'], ['green', 'yellow']), # array of colors + ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) # RGB values + ]) + def test_scatter_c_facecolor_warning(self, colors): + warn_match = ( + "You passed both c and facecolor/facecolors for the markers. " + "c has precedence over facecolor/facecolors. This behavior may " + "change in the future." + ) + fig, ax = plt.subplots() + x = [0, 1] if len(colors[0]) == 2 else [0] + y = x + with pytest.warns(UserWarning, match=warn_match): + ax.scatter(x, y, c=colors[0], facecolors=colors[1]) + def test_scatter_unfilled(self): coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'], marker=mmarkers.MarkerStyle('o', fillstyle='none'), From fcf6ce2589474fb2be1d35c444e5af1d8d5e8957 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:12:44 -0500 Subject: [PATCH 15/19] Corrected flake8 linting objections. --- lib/matplotlib/tests/test_axes.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index d2bc0f33ebb8..f4025c103fab 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2779,12 +2779,12 @@ def test_scatter_color_warning(self, kwargs): plt.scatter([], [], c=[], **kwargs) plt.scatter([1, 2], [3, 4], c=[4, 5], **kwargs) - @pytest.mark.parametrize('colors', - [ - ('red', 'blue'), # simple string colors - (['red', 'blue'], ['green', 'yellow']), # array of colors - ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) # RGB values - ]) + @pytest.mark.parametrize('colors', + [ + ('red', 'blue'), + (['red', 'blue'], ['green', 'yellow']), + ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) + ]) def test_scatter_c_facecolor_warning(self, colors): warn_match = ( "You passed both c and facecolor/facecolors for the markers. " @@ -2796,7 +2796,7 @@ def test_scatter_c_facecolor_warning(self, colors): y = x with pytest.warns(UserWarning, match=warn_match): ax.scatter(x, y, c=colors[0], facecolors=colors[1]) - + def test_scatter_unfilled(self): coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'], marker=mmarkers.MarkerStyle('o', fillstyle='none'), From 96019754b07f48c3ff3058007bc3a1a7637740ae Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:12:17 -0500 Subject: [PATCH 16/19] Added direct test of _parse method to attempt to make Codecov happy. --- lib/matplotlib/tests/test_axes.py | 67 ++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index f4025c103fab..296dafd6b00d 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2779,24 +2779,6 @@ def test_scatter_color_warning(self, kwargs): plt.scatter([], [], c=[], **kwargs) plt.scatter([1, 2], [3, 4], c=[4, 5], **kwargs) - @pytest.mark.parametrize('colors', - [ - ('red', 'blue'), - (['red', 'blue'], ['green', 'yellow']), - ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) - ]) - def test_scatter_c_facecolor_warning(self, colors): - warn_match = ( - "You passed both c and facecolor/facecolors for the markers. " - "c has precedence over facecolor/facecolors. This behavior may " - "change in the future." - ) - fig, ax = plt.subplots() - x = [0, 1] if len(colors[0]) == 2 else [0] - y = x - with pytest.warns(UserWarning, match=warn_match): - ax.scatter(x, y, c=colors[0], facecolors=colors[1]) - def test_scatter_unfilled(self): coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'], marker=mmarkers.MarkerStyle('o', fillstyle='none'), @@ -3080,6 +3062,55 @@ def get_next_color(): c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color) +# Warning message tested in the next two tests. +WARN_MSG = ( + "You passed both c and facecolor/facecolors for the markers. " + "c has precedence over facecolor/facecolors. This behavior may " + "change in the future." +) +# Test cases shared between direct and integration tests +COLOR_TEST_CASES = [ + ('red', 'blue'), + (['red', 'blue'], ['green', 'yellow']), + ([[1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 1, 0]]) +] + + +@pytest.mark.parametrize('c, facecolor', COLOR_TEST_CASES) +def test_parse_c_facecolor_warning_direct(c, facecolor): + """Test the internal _parse_scatter_color_args method directly.""" + def get_next_color(): + return 'blue' + + # Test with facecolors (plural) + with pytest.warns(UserWarning, match=WARN_MSG): + mpl.axes.Axes._parse_scatter_color_args( + c=c, edgecolors=None, kwargs={'facecolors': facecolor}, + xsize=2, get_next_color_func=get_next_color) + + # Test with facecolor (singular) + with pytest.warns(UserWarning, match=WARN_MSG): + mpl.axes.Axes._parse_scatter_color_args( + c=c, edgecolors=None, kwargs={'facecolor': facecolor}, + xsize=2, get_next_color_func=get_next_color) + + +@pytest.mark.parametrize('c, facecolor', COLOR_TEST_CASES) +def test_scatter_c_facecolor_warning_integration(c, facecolor): + """Test the warning through the actual scatter plot creation.""" + fig, ax = plt.subplots() + x = [0, 1] if isinstance(c, (list, tuple)) else [0] + y = x + + # Test with facecolors (plural) + with pytest.warns(UserWarning, match=WARN_MSG): + ax.scatter(x, y, c=c, facecolors=facecolor) + + # Test with facecolor (singular) + with pytest.warns(UserWarning, match=WARN_MSG): + ax.scatter(x, y, c=c, facecolor=facecolor) + + def test_as_mpl_axes_api(): # tests the _as_mpl_axes api class Polar: From a18364c78bd6b93b33a99bcf7c5f7dcbf1a18ab4 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:10:05 -0500 Subject: [PATCH 17/19] Added pragma tags for code coverage to ignore unused but untested simplified return. --- lib/matplotlib/tests/test_axes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 296dafd6b00d..faebce873ae2 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2920,7 +2920,7 @@ def test_scatter_different_shapes(self, fig_test, fig_ref): @pytest.mark.parametrize('c_case, re_key', params_test_scatter_c) def test_scatter_c(self, c_case, re_key): def get_next_color(): - return 'blue' # currently unused + return 'blue' # currently unused # pragma: no cover xsize = 4 # Additional checking of *c* (introduced in #11383). @@ -3014,7 +3014,7 @@ def _params(c=None, xsize=2, *, edgecolors=None, **kwargs): ]) def test_parse_scatter_color_args(params, expected_result): def get_next_color(): - return 'blue' # currently unused + return 'blue' # currently unused # pragma: no cover c, colors, _edgecolors = mpl.axes.Axes._parse_scatter_color_args( *params, get_next_color_func=get_next_color) @@ -3041,7 +3041,7 @@ def get_next_color(): ]) def test_parse_scatter_color_args_edgecolors(kwargs, expected_edgecolors): def get_next_color(): - return 'blue' # currently unused + return 'blue' # currently unused # pragma: no cover c = kwargs.pop('c', None) edgecolors = kwargs.pop('edgecolors', None) @@ -3053,7 +3053,7 @@ def get_next_color(): def test_parse_scatter_color_args_error(): def get_next_color(): - return 'blue' # currently unused + return 'blue' # currently unused # pragma: no cover with pytest.raises(ValueError, match="RGBA values should be within 0-1 range"): @@ -3080,7 +3080,7 @@ def get_next_color(): def test_parse_c_facecolor_warning_direct(c, facecolor): """Test the internal _parse_scatter_color_args method directly.""" def get_next_color(): - return 'blue' + return 'blue' # currently unused # pragma: no cover # Test with facecolors (plural) with pytest.warns(UserWarning, match=WARN_MSG): From 7c01a7b2dc4ff837dffbcb703ad632e7889bc02f Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Wed, 4 Dec 2024 10:11:02 -0500 Subject: [PATCH 18/19] Tim's suggestion for naming test's list of colors. Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/tests/test_legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index c8963422fdd5..460a93f4828f 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -969,7 +969,7 @@ def test_legend_pathcollection_labelcolor_markfacecolor_cmap(): # test the labelcolor for labelcolor='markerfacecolor' on PathCollection # with colormaps fig, ax = plt.subplots() - colormap = mpl.cm.viridis(np.random.rand(10)) + colors = mpl.cm.viridis(np.random.rand(10)) ax.scatter( np.arange(10), np.arange(10), From fd3d9db791b14432dc0f6477c706de3e3f93dee9 Mon Sep 17 00:00:00 2001 From: "Nathan G. Wiseman" <55075327+NGWi@users.noreply.github.com> Date: Wed, 4 Dec 2024 10:24:12 -0500 Subject: [PATCH 19/19] Corrected call to changed variable name. --- lib/matplotlib/tests/test_legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 460a93f4828f..8e12772ef5ab 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -974,7 +974,7 @@ def test_legend_pathcollection_labelcolor_markfacecolor_cmap(): np.arange(10), np.arange(10), label='#1', - c=colormap + c=colors ) leg = ax.legend(labelcolor='markerfacecolor')