Fix Axes.grid() to respect alpha in color tuples #30849
Open
+45
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR summary
This PR fixes issue #22231 where
ax.grid(color=(R, G, B, A))ignores the alpha channel in color tuples, causing gridlines to appear fully opaque instead of with the specified transparency.Problem: When calling
ax.grid(color=(0.8, 0.8, 0.8, 0.2)), the gridlines appeared with alpha=1.0 instead of alpha=0.2.Root Cause: The issue occurred in two places in
lib/matplotlib/axis.py:Tick.__init__: When creating ticks with a color containing alpha, the code didn't extract the alpha value from the color tupleTick._apply_params: When updating gridlines viagrid(), color tuples with alpha weren't being decomposedSolution: Extract alpha from color tuples using
mcolors.to_rgba()and set color (RGB) and alpha separately, ensuring proper alpha precedence: explicit alpha parameter > color tuple alpha > rcParams.Minimum example demonstrating the fix:
closes #22231
PR checklist
test_grid_color_with_alpha)