Skip to content

Partial fix for grid alpha #22234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from matplotlib import _api
import matplotlib.artist as martist
import matplotlib.cbook as cbook
import matplotlib.colors as mcolors
import matplotlib.lines as mlines
import matplotlib.scale as mscale
import matplotlib.text as mtext
Expand Down Expand Up @@ -143,7 +144,12 @@ def __init__(
grid_linestyle = mpl.rcParams["grid.linestyle"]
if grid_linewidth is None:
grid_linewidth = mpl.rcParams["grid.linewidth"]
if grid_alpha is None:
if grid_alpha is None and not mcolors._has_alpha_channel(grid_color):
# alpha precedence: kwarg > color alpha > rcParams['grid.alpha']
# Note: only resolve to rcParams if the color does not have alpha
# otherwise `grid(color=(1, 1, 1, 0.5))` would work like
# grid(color=(1, 1, 1, 0.5), alpha=rcParams['grid.alpha'])
# so the that the rcParams default would override color alpha.
grid_alpha = mpl.rcParams["grid.alpha"]
grid_kw = {k[5:]: v for k, v in kwargs.items()}

Expand Down Expand Up @@ -1413,7 +1419,7 @@ def grid(self, visible=None, which='major', **kwargs):
visible = True
which = which.lower()
_api.check_in_list(['major', 'minor', 'both'], which=which)
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
gridkw = {f'grid_{name}': value for name, value in kwargs.items()}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No functional change, only better formatting for readability.

if which in ['minor', 'both']:
gridkw['gridOn'] = (not self._minor_tick_kw['gridOn']
if visible is None else visible)
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ def is_color_like(c):
return True


def _has_alpha_channel(c):
"""Return whether *c* is a color with an alpha channel."""
# 4-element sequences are interpreted as r, g, b, a
return not isinstance(c, str) and len(c) == 4


def _check_color_like(**kwargs):
"""
For each *key, value* pair in *kwargs*, check that *value* is color-like.
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,15 @@ def test_colormap_reversing(name):
assert_array_almost_equal(cmap(np.nan), cmap_r(np.nan))


def test_has_alpha_channel():
assert mcolors._has_alpha_channel((0, 0, 0, 0))
assert mcolors._has_alpha_channel([1, 1, 1, 1])
assert not mcolors._has_alpha_channel('blue') # 4-char string!
assert not mcolors._has_alpha_channel('0.25')
assert not mcolors._has_alpha_channel('r')
assert not mcolors._has_alpha_channel((1, 0, 0))


def test_cn():
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
['blue', 'r'])
Expand Down