8000 Support individual styling of major and minor grid through rcParams by konmenel · Pull Request #29481 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Support individual styling of major and minor grid through rcParams #29481

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
24 changes: 20 additions & 4 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,32 @@ def __init__(
zorder = mlines.Line2D.zorder
self._zorder = zorder

grid_color = mpl._val_or_rc(grid_color, "grid.color")
grid_linestyle = mpl._val_or_rc(grid_linestyle, "grid.linestyle")
grid_linewidth = mpl._val_or_rc(grid_linewidth, "grid.linewidth")
grid_color = (
mpl._val_or_rc(grid_color, "grid.color")
if mpl.rcParams[f"grid.{major_minor}.color"] == "none"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if mpl.rcParams[f"grid.{major_minor}.color"] == "none"
if mpl.rcParams[f"grid.{major_minor}.color"] is None

see comment on validation

else mpl._val_or_rc(grid_color, f"grid.{major_minor}.color")
)
grid_linestyle = (
mpl._val_or_rc(grid_linestyle, "grid.linestyle")
if mpl.rcParams[f"grid.{major_minor}.linestyle"] == "none"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if mpl.rcParams[f"grid.{major_minor}.linestyle"] == "none"
if mpl.rcParams[f"grid.{major_minor}.linestyle"] is None

see comment on validation

else mpl._val_or_rc(grid_linestyle, f"grid.{major_minor}.linestyle")
)
grid_linewidth = (
mpl._val_or_rc(grid_linewidth, "grid.linewidth")
if mpl.rcParams[f"grid.{major_minor}.linewidth"] is None
else mpl._val_or_rc(grid_linewidth, f"grid.{major_minor}.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.rcParams["grid.alpha"]
grid_alpha = (
mpl.rcParams["grid.alpha"]
if f"grid.{major_minor}.alpha" not in mpl.rcParams
Copy link
Member

Choose a reason for hiding this comment

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

Why is the logic here different (hasattr) compared to the other parameters (is None)?

Copy link
Author
@konmenel konmenel Jan 27, 2025

Choose a reason for hiding this comment

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

Honestly, I have no idea so the only explanation is stupidity. Furthermore, I think this is a bug because grid.minor/major.alpha is always in rcParams. I will fix this so that is the same with the rest.

else mpl.rcParams[f"grid.{major_minor}.alpha"]
)
grid_kw = {k[5:]: v for k, v in kwargs.items()}

self.tick1line = mlines.Line2D(
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@
#grid.linewidth: 0.8 # in points
#grid.alpha: 1.0 # transparency, between 0.0 and 1.0

#grid.major.color: None # If None defaults to grid.color
#grid.major.linestyle: None # If None defaults to grid.linestyle
#grid.major.linewidth: None # If None defaults to grid.linewidth
#grid.major.alpha: None # If None defaults to grid.alpha

#grid.minor.color: None # If None defaults to grid.color
#grid.minor.linestyle: None # If None defaults to grid.linestyle
#grid.minor.linewidth: None # If None defaults to grid.linewidth
#grid.minor.alpha: None # If None defaults to grid.alpha


## ***************************************************************************
## * LEGEND *
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,16 @@ def _convert_validator_spec(key, conv):
"grid.linewidth": validate_float, # in points
"grid.alpha": validate_float,

"grid.major.color": validate_color, # grid color
"grid.major.linestyle": _validate_linestyle, # solid
Copy link
Member

Choose a reason for hiding this comment

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

We have to be a bit more precise with these. The string "none" is a valid value for both. We should not repurpose it here with the meaning not defined. In the parsed rcParams dict, we need to be able to have both values None and "none".
Since matplotlibrc is purely text based, we have to distinguish by capitalization. 'None' must be parsed to None, and 'none' must be parsed to "none". Please create respective validate_color_or_None and validate_linestyle_or_None functions for that.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good. I was thinking of doing something like that but I saw that the functions parse the "None" and "none", so I didn't know if I should introduce a new function or not. Nevertheless, this should be easy.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it’s a bit unfortunate 67E6 that the existing parameters are permissive wrt. to capitalization. But the new ones have to distinguish. We may deprecate accepting None for the other variants. But that’d be for a separate PR.

"grid.major.linewidth": validate_float_or_None, # in points
"grid.major.alpha": validate_float_or_None,

"grid.minor.color": validate_color, # grid color
"grid.minor.linestyle": _validate_linestyle, # solid
"grid.minor.linewidth": validate_float_or_None, # in points
"grid.minor.alpha": validate_float_or_None,

## figure props
# figure title
"figure.titlesize": validate_fontsize,
Expand Down
Loading
0