@@ -140,17 +140,20 @@ def __init__(
140140 f"grid.{ major_minor } .linewidth" ,
141141 "grid.linewidth" ,
142142 )
143- if grid_alpha is None and not mcolors ._has_alpha_channel (grid_color ):
144- # alpha precedence: kwarg > color alpha > rcParams['grid.alpha']
145- # Note: only resolve to rcParams if the color does not have alpha
146- # otherwise `grid(color=(1, 1, 1, 0.5))` would work like
147- # grid(color=(1, 1, 1, 0.5), alpha=rcParams['grid.alpha'])
148- # so the that the rcParams default would override color alpha.
149- grid_alpha = mpl ._val_or_rc (
150- # grid_alpha is None so we can use the first key
151- mpl .rcParams [f"grid.{ major_minor } .alpha" ],
152- "grid.alpha" ,
153- )
143+ if grid_alpha is None :
144+ if mcolors ._has_alpha_channel (grid_color ):
145+ # Extract alpha from the color
146+ # alpha precedence: kwarg > color alpha > rcParams['grid.alpha']
147+ rgba = mcolors .to_rgba (grid_color )
148+ grid_color = rgba [:3 ] # RGB only
149+ grid_alpha = rgba [3 ] # Alpha from color
150+ else :
151+ # No alpha in color, use rcParams
152+ grid_alpha = mpl ._val_or_rc (
153+ # grid_alpha is None so we can use the first key
154+ mpl .rcParams [f"grid.{ major_minor } .alpha" ],
155+ "grid.alpha" ,
156+ )
154157
155158 grid_kw = {k [5 :]: v for k , v in kwargs .items () if k != "rotation_mode" }
156159
@@ -348,6 +351,15 @@ def _apply_params(self, **kwargs):
348351
349352 grid_kw = {k [5 :]: v for k , v in kwargs .items ()
350353 if k in _gridline_param_names }
354+ # If grid_color has an alpha channel and grid_alpha is not explicitly
355+ # set, extract the alpha from the color.
356+ if 'color' in grid_kw and 'alpha' not in grid_kw :
357+ grid_color = grid_kw ['color' ]
358+ if mcolors ._has_alpha_channel (grid_color ):
359+ # Convert to rgba to extract alpha
360+ rgba = mcolors .to_rgba (grid_color )
361+ grid_kw ['color' ] = rgba [:3 ] # RGB only
362+ grid_kw ['alpha' ] = rgba [3 ] # Alpha channel
351363 self .gridline .set (** grid_kw )
352364
353365 def update_position (self , loc ):
0 commit comments