Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df968b5

Browse files
chaoyihutimhoffmQuLogic
authored
Make sure custom alpha param does not change 'none' colors in a list of colors (#27845)
* fix issue 27839, make sure alpha param does not affect none colors Apply review feedbacks Co-authored-by: Tim Hoffmann <[email protected]> * Update lib/matplotlib/colors.py Co-authored-by: Elliott Sales de Andrade <[email protected]> --------- Co-authored-by: Tim Hoffmann <[email protected]> Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent 3813a63 commit df968b5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/matplotlib/colors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,11 @@ def to_rgba_array(c, alpha=None):
506506

507507
if alpha is not None:
508508
rgba[:, 3] = alpha
509+
if isinstance(c, Sequence):
510+
# ensure that an explicit alpha does not overwrite full transparency
511+
# for "none"
512+
none_mask = [cbook._str_equal(cc, "none") for cc in c]
513+
rgba[:, 3][none_mask] = 0
509514
return rgba
510515

511516

lib/matplotlib/tests/test_colors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import matplotlib.scale as mscale
2020
from matplotlib.rcsetup import cycler
2121
from matplotlib.testing.decorators import image_comparison, check_figures_equal
22+
from matplotlib.colors import to_rgba_array
2223

2324

2425
@pytest.mark.parametrize('N, result', [
@@ -1686,3 +1687,13 @@ def test_set_cmap_mismatched_name():
16861687
cmap_returned = plt.get_cmap("wrong-cmap")
16871688
assert cmap_returned == cmap
16881689
assert cmap_returned.name == "wrong-cmap"
1690+
1691+
1692+
def test_to_rgba_array_none_color_with_alpha_param():
1693+
# effective alpha for color "none" must always be 0 to achieve a vanishing color
1694+
# even explicit alpha must be ignored
1695+
c = ["blue", "none"]
1696+
alpha = [1, 1]
1697+
assert_array_equal(
1698+
to_rgba_array(c, alpha), [[0., 0., 1., 1.], [0., 0., 0., 0.]]
1699+
)

0 commit comments

Comments
 (0)