|
12 | 12 | from matplotlib import _api |
13 | 13 | import matplotlib.artist as martist |
14 | 14 | import matplotlib.cbook as cbook |
| 15 | +import matplotlib.colors as mcolors |
15 | 16 | import matplotlib.lines as mlines |
16 | 17 | import matplotlib.scale as mscale |
17 | 18 | import matplotlib.text as mtext |
@@ -143,7 +144,12 @@ def __init__( |
143 | 144 | grid_linestyle = mpl.rcParams["grid.linestyle"] |
144 | 145 | if grid_linewidth is None: |
145 | 146 | 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. |
147 | 153 | grid_alpha = mpl.rcParams["grid.alpha"] |
148 | 154 | grid_kw = {k[5:]: v for k, v in kwargs.items()} |
149 | 155 |
|
@@ -1413,7 +1419,7 @@ def grid(self, visible=None, which='major', **kwargs): |
1413 | 1419 | visible = True |
1414 | 1420 | which = which.lower() |
1415 | 1421 | _api.check_in_list(['major', 'minor', 'both'], which=which) |
1416 | | - gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()} |
| 1422 | + gridkw = {f'grid_{name}': value for name, value in kwargs.items()} |
1417 | 1423 | if which in ['minor', 'both']: |
1418 | 1424 | gridkw['gridOn'] = (not self._minor_tick_kw['gridOn'] |
1419 | 1425 | if visible is None else visible) |
|
0 commit comments