Skip to content

Commit bba8e9e

Browse files
authored
Merge pull request matplotlib#22234 from timhoffm/grid-color-alpha
Partial fix for grid alpha
2 parents 541a786 + a3f0abc commit bba8e9e

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/matplotlib/axis.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from matplotlib import _api
1313
import matplotlib.artist as martist
1414
import matplotlib.cbook as cbook
15+
import matplotlib.colors as mcolors
1516
import matplotlib.lines as mlines
1617
import matplotlib.scale as mscale
1718
import matplotlib.text as mtext
@@ -143,7 +144,12 @@ def __init__(
143144
grid_linestyle = mpl.rcParams["grid.linestyle"]
144145
if grid_linewidth is None:
145146
grid_linewidth = mpl.rcParams["grid.linewidth"]
146-
if grid_alpha is None:
147+
if grid_alpha is None and not mcolors._has_alpha_channel(grid_color):
148+
# alpha precedence: kwarg > color alpha > rcParams['grid.alpha']
149+
# Note: only resolve to rcParams if the color does not have alpha
150+
# otherwise `grid(color=(1, 1, 1, 0.5))` would work like
151+
# grid(color=(1, 1, 1, 0.5), alpha=rcParams['grid.alpha'])
152+
# so the that the rcParams default would override color alpha.
147153
grid_alpha = mpl.rcParams["grid.alpha"]
148154
grid_kw = {k[5:]: v for k, v in kwargs.items()}
149155

@@ -1498,7 +1504,7 @@ def grid(self, visible=None, which='major', **kwargs):
14981504
visible = True
14991505
which = which.lower()
15001506
_api.check_in_list(['major', 'minor', 'both'], which=which)
1501-
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
1507+
gridkw = {f'grid_{name}': value for name, value in kwargs.items()}
15021508
if which in ['minor', 'both']:
15031509
gridkw['gridOn'] = (not self._minor_tick_kw['gridOn']
15041510
if visible is None else visible)

lib/matplotlib/colors.py

+6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ def is_color_like(c):
121121
return True
122122

123123

124+
def _has_alpha_channel(c):
125+
"""Return whether *c* is a color with an alpha channel."""
126+
# 4-element sequences are interpreted as r, g, b, a
127+
return not isinstance(c, str) and len(c) == 4
128+
129+
124130
def _check_color_like(**kwargs):
125131
"""
126132
For each *key, value* pair in *kwargs*, check that *value* is color-like.

lib/matplotlib/tests/test_colors.py

+9
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,15 @@ def test_colormap_reversing(name):
11661166
assert_array_almost_equal(cmap(np.nan), cmap_r(np.nan))
11671167

11681168

1169+
def test_has_alpha_channel():
1170+
assert mcolors._has_alpha_channel((0, 0, 0, 0))
1171+
assert mcolors._has_alpha_channel([1, 1, 1, 1])
1172+
assert not mcolors._has_alpha_channel('blue') # 4-char string!
1173+
assert not mcolors._has_alpha_channel('0.25')
1174+
assert not mcolors._has_alpha_channel('r')
1175+
assert not mcolors._has_alpha_channel((1, 0, 0))
1176+
1177+
11691178
def test_cn():
11701179
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
11711180
['blue', 'r'])

0 commit comments

Comments
 (0)