From 469faf60e8196c85f070939ba9491bdc088901e2 Mon Sep 17 00:00:00 2001 From: Ines Cachola Date: Fri, 28 Mar 2025 13:14:41 +0000 Subject: [PATCH 1/5] Fix #29780 : color of errorbar caps affected by markeredgecolor To solve this, we needed to ensure that the caps would use the specified ecolor independently of the global mpl.rcParams['lines.markeredgecolor']. This was achieved by setting the marker edge color for the caps separately, rather than relying on the global configuration. --- lib/matplotlib/axes/_axes.py | 2 +- lib/matplotlib/tests/test_axes.py | 32 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index c72e52ca41f1..f6208438cd99 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3723,7 +3723,7 @@ def _upcast_err(err): 'zorder', 'rasterized'): if key in kwargs: eb_cap_style[key] = kwargs[key] - eb_cap_style['color'] = ecolor + eb_cap_style["markeredgecolor"] = ecolor barcols = [] caplines = {'x': [], 'y': []} diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index d7002be675c6..396106131a4a 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -9685,3 +9685,35 @@ def test_bar_shape_mismatch(): ) with pytest.raises(ValueError, match=error_message): plt.bar(x, height) + + +def test_caps_color(): + + # Creates a simple plot with error bars and a specified ecolor + x = np.linspace(0, 10, 10) + y = np.sin(x) + yerr = 0.1 + mpl.rcParams['lines.markeredgecolor'] = 'green' + ecolor = 'red' + + fig, ax = plt.subplots() + errorbars = ax.errorbar(x, y, yerr=yerr, ecolor=ecolor, fmt='o', capsize=5) + + # Tests if the caps have the specified color + for cap in errorbars[2]: + assert mcolors.same_color(cap.get_edgecolor(), ecolor) + +def test_caps_no_ecolor(): + + # Creates a simple plot with error bars without specifying ecolor + x = np.linspace(0, 10, 10) + y = np.sin(x) + yerr = 0.1 + mpl.rcParams['lines.markeredgecolor'] = 'green' + + fig, ax = plt.subplots() + errorbars = ax.errorbar(x, y, yerr=yerr, fmt='o', capsize=5) + + # Tesrts if the caps have the default color (blue) + for cap in errorbars[2]: + assert mcolors.same_color(cap.get_edgecolor(), "blue") From b8c9a728de36a13356be47c1dde99940b9f64cb8 Mon Sep 17 00:00:00 2001 From: Ines Cachola Date: Sat, 19 Apr 2025 12:58:54 +0100 Subject: [PATCH 2/5] Taking whitespaces --- lib/matplotlib/tests/test_axes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 672ef692adcd..eaf11a2969c8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -9734,7 +9734,7 @@ def test_bar_shape_mismatch(): def test_caps_color(): - + # Creates a simple plot with error bars and a specified ecolor x = np.linspace(0, 10, 10) y = np.sin(x) @@ -9749,8 +9749,9 @@ def test_caps_color(): for cap in errorbars[2]: assert mcolors.same_color(cap.get_edgecolor(), ecolor) + def test_caps_no_ecolor(): - + # Creates a simple plot with error bars without specifying ecolor x = np.linspace(0, 10, 10) y = np.sin(x) From 9e0df6ca846f941825aec40435e9ce7214477113 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Mon, 28 Apr 2025 16:56:22 +0200 Subject: [PATCH 3/5] Update lib/matplotlib/tests/test_axes.py --- lib/matplotlib/tests/test_axes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index d93bf8d0946c..aefb0d007c70 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -9772,4 +9772,3 @@ def test_pie_non_finite_values(): with pytest.raises(ValueError, match='Wedge sizes must be finite numbers'): ax.pie(df, labels=['A', 'B', 'C']) - From 0984d4c1784961c6eac0f5abf70d1748b5194da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?In=C3=AAs=20Cachola?= Date: Fri, 9 May 2025 13:15:58 +0100 Subject: [PATCH 4/5] Update lib/matplotlib/tests/test_axes.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/tests/test_axes.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index aefb0d007c70..912b45a07c97 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -9737,13 +9737,11 @@ def test_caps_color(): # Creates a simple plot with error bars and a specified ecolor x = np.linspace(0, 10, 10) - y = np.sin(x) - yerr = 0.1 mpl.rcParams['lines.markeredgecolor'] = 'green' ecolor = 'red' fig, ax = plt.subplots() - errorbars = ax.errorbar(x, y, yerr=yerr, ecolor=ecolor, fmt='o', capsize=5) + errorbars = ax.errorbar(x, sin(x), yerr=0.1, ecolor=ecolor) # Tests if the caps have the specified color for cap in errorbars[2]: From 63abf89541445e45e1450a6b525086638dc25afa Mon Sep 17 00:00:00 2001 From: Ines Cachola Date: Mon, 12 May 2025 14:07:16 +0100 Subject: [PATCH 5/5] Alteration of the tests --- lib/matplotlib/tests/test_axes.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 912b45a07c97..3946c67e3f39 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -9741,7 +9741,7 @@ def test_caps_color(): ecolor = 'red' fig, ax = plt.subplots() - errorbars = ax.errorbar(x, sin(x), yerr=0.1, ecolor=ecolor) + errorbars = ax.errorbar(x, np.sin(x), yerr=0.1, ecolor=ecolor) # Tests if the caps have the specified color for cap in errorbars[2]: @@ -9752,14 +9752,11 @@ def test_caps_no_ecolor(): # Creates a simple plot with error bars without specifying ecolor x = np.linspace(0, 10, 10) - y = np.sin(x) - yerr = 0.1 mpl.rcParams['lines.markeredgecolor'] = 'green' - fig, ax = plt.subplots() - errorbars = ax.errorbar(x, y, yerr=yerr, fmt='o', capsize=5) + errorbars = ax.errorbar(x, np.sin(x), yerr=0.1) - # Tesrts if the caps have the default color (blue) + # Tests if the caps have the default color (blue) for cap in errorbars[2]: assert mcolors.same_color(cap.get_edgecolor(), "blue")