🌐 AI搜索 & 代理 主页
Skip to content
Open
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
34 changes: 23 additions & 11 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,20 @@ def __init__(
f"grid.{major_minor}.linewidth",
"grid.linewidth",
)
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._val_or_rc(
# grid_alpha is None so we can use the first key
mpl.rcParams[f"grid.{major_minor}.alpha"],
"grid.alpha",
)
if grid_alpha is None:
if mcolors._has_alpha_channel(grid_color):
# Extract alpha from the color
# alpha precedence: kwarg > color alpha > rcParams['grid.alpha']
rgba = mcolors.to_rgba(grid_color)
grid_color = rgba[:3] # RGB only
grid_alpha = rgba[3] # Alpha from color
else:
# No alpha in color, use rcParams
grid_alpha = mpl._val_or_rc(
# grid_alpha is None so we can use the first key
mpl.rcParams[f"grid.{major_minor}.alpha"],
"grid.alpha",
)

grid_kw = {k[5:]: v for k, v in kwargs.items() if k != "rotation_mode"}

Expand Down Expand Up @@ -348,6 +351,15 @@ def _apply_params(self, **kwargs):

grid_kw = {k[5:]: v for k, v in kwargs.items()
if k in _gridline_param_names}
# If grid_color has an alpha channel and grid_alpha is not explicitly
# set, extract the alpha from the color.
if 'color' in grid_kw and 'alpha' not in grid_kw:
grid_color = grid_kw['color']
if mcolors._has_alpha_channel(grid_color):
# Convert to rgba to extract alpha
rgba = mcolors.to_rgba(grid_color)
grid_kw['color'] = rgba[:3] # RGB only
grid_kw['alpha'] = rgba[3] # Alpha channel
self.gridline.set(**grid_kw)

def update_position(self, loc):
Expand Down
22 changes: 22 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6133,6 +6133,28 @@
assert not ax.xaxis.majorTicks[0].gridline.get_visible()


def test_grid_color_with_alpha():
"""Test that grid(color=(..., alpha)) respects the alpha value."""
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])

Check warning on line 6140 in lib/matplotlib/tests/test_axes.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/tests/test_axes.py" range:{start:{line:6140 column:1} end:{line:6140 column:5}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:6140 column:1} end:{line:6140 column:5}}}
# Test 1: Color tuple with alpha
ax.grid(True, color=(0.5, 0.6, 0.7, 0.3))
fig.canvas.draw()

Check warning on line 6144 in lib/matplotlib/tests/test_axes.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/tests/test_axes.py" range:{start:{line:6144 column:1} end:{line:6144 column:5}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:6144 column:1} end:{line:6144 column:5}}}
# Check that alpha is extracted from color tuple
for tick in ax.xaxis.get_major_ticks():
assert tick.gridline.get_alpha() == 0.3, \
f"Expected alpha=0.3, got {tick.gridline.get_alpha()}"
# Color should be RGB only, without alpha component
color = tick.gridline.get_color()
assert len(color) in (3, 4), f"Unexpected color format: {color}"

Check warning on line 6152 in lib/matplotlib/tests/test_axes.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/tests/test_axes.py" range:{start:{line:6152 column:1} end:{line:6152 column:9}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:6152 column:1} end:{line:6152 column:9}}}
for tick in ax.yaxis.get_major_ticks():
assert tick.gridline.get_alpha() == 0.3, \
f"Expected alpha=0.3, got {tick.gridline.get_alpha()}"


def test_reset_grid():
fig, ax = plt.subplots()
ax.tick_params(reset=True, which='major', labelsize=10)
Expand Down
Loading