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 7 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
47 changes: 43 additions & 4 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def __init__(

name = self.__name__
major_minor = "major" if major else "minor"
xaxis_yaxis = "xaxis" if name == "xtick" else "yaxis"
self._size = mpl._val_or_rc(size, f"{name}.{major_minor}.size")
self._width = mpl._val_or_rc(width, f"{name}.{major_minor}.width")
self._base_pad = mpl._val_or_rc(pad, f"{name}.{major_minor}.pad")
Expand All @@ -124,16 +125,54 @@ 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, f"grid.{xaxis_yaxis}.{major_minor}.color"
)
if mpl.rcParams[f"grid.{xaxis_yaxis}.{major_minor}.color"] is not None
else (
mpl._val_or_rc(grid_color, f"grid.{major_minor}.color")
if mpl.rcParams[f"grid.{major_minor}.color"] is not None
else mpl._val_or_rc(grid_color, "grid.color")
)
)
grid_linestyle = (
mpl._val_or_rc(
grid_linestyle, f"grid.{xaxis_yaxis}.{major_minor}.linestyle"
)
if mpl.rcParams[f"grid.{xaxis_yaxis}.{major_minor}.linestyle"] is not None
else (
mpl._val_or_rc(grid_linestyle, f"grid.{major_minor}.linestyle")
if mpl.rcParams[f"grid.{major_minor}.linestyle"] is not None
else mpl._val_or_rc(grid_linestyle, "grid.linestyle")
)
)
grid_linewidth = (
mpl._val_or_rc(
grid_linewidth, f"grid.{xaxis_yaxis}.{major_minor}.linewidth"
)
if mpl.rcParams[f"grid.{xaxis_yaxis}.{major_minor}.linewidth"] is not None
else (
mpl._val_or_rc(grid_linewidth, f"grid.{major_minor}.linewidth")
if mpl.rcParams[f"grid.{major_minor}.linewidth"] is not None
else mpl._val_or_rc(grid_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.rcParams["grid.alpha"]
grid_alpha = (
mpl.rcParams[f"grid.{xaxis_yaxis}.{major_minor}.alpha"]
if mpl.rcParams[f"grid.{xaxis_yaxis}.{major_minor}.alpha"] is not None
else (
mpl.rcParams[f"grid.{major_minor}.alpha"]
if mpl.rcParams[f"grid.{major_minor}.alpha"] is not None
else mpl.rcParams["grid.alpha"]
)
)
grid_kw = {k[5:]: v for k, v in kwargs.items()}

self.tick1line = mlines.Line2D(
Expand Down
30 changes: 30 additions & 0 deletions lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,36 @@
#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

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

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

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

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


## ***************************************************************************
## * LEGEND *
Expand Down
43 changes: 43 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@
raise ValueError(f'{s!r} does not look like a color arg')


def validate_color_or_None(s):
if s is None or cbook._str_equal(s, "None"):
return None
return validate_color(s)


validate_colorlist = _listify_validator(
validate_color, allow_stringlist=True, doc='return a list of colorspecs')

Expand Down Expand Up @@ -514,6 +520,13 @@
8000 raise ValueError(f"linestyle {ls!r} is not a valid on-off ink sequence.")


def _validate_linestyle_or_None(ls):
if ls is None or cbook._str_equal(ls, "None"):
return None

return _validate_linestyle(ls)

Check warning on line 527 in lib/matplotlib/rcsetup.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/rcsetup.py#L527

Added line #L527 was not covered by tests


validate_fillstyle = ValidateInStrings(
'markers.fillstyle', ['full', 'left', 'right', 'bottom', 'top', 'none'])

Expand Down Expand Up @@ -1237,6 +1250,36 @@
"grid.linewidth": validate_float, # in points
"grid.alpha": validate_float,

"grid.major.color": validate_color_or_None, # grid color
"grid.major.linestyle": _validate_linestyle_or_None, # solid
"grid.major.linewidth": validate_float_or_None, # in points
"grid.major.alpha": validate_float_or_None,

"grid.minor.color": validate_color_or_None, # grid color
"grid.minor.linestyle": _validate_linestyle_or_None, # solid
"grid.minor.linewidth": validate_float_or_None, # in points
"grid.minor.alpha": validate_float_or_None,

"grid.xaxis.major.color": validate_color_or_None, # grid color
"grid.xaxis.major.linestyle": _validate_linestyle_or_None, # solid
"grid.xaxis.major.linewidth": validate_float_or_None, # in points
"grid.xaxis.major.alpha": validate_float_or_None,

"grid.xaxis.minor.color": validate_color_or_None, # grid color
"grid.xaxis.minor.linestyle": _validate_linestyle_or_None, # solid
"grid.xaxis.minor.linewidth": validate_float_or_None, # in points
"grid.xaxis.minor.alpha": validate_float_or_None,

"grid.yaxis.major.color": validate_color_or_None, # grid color
"grid.yaxis.major.linestyle": _validate_linestyle_or_None, # solid
"grid.yaxis.major.linewidth": validate_float_or_None, # in points
"grid.yaxis.major.alpha": validate_float_or_None,

"grid.yaxis.minor.color": validate_color_or_None, # grid color
"grid.yaxis.minor.linestyle": _validate_linestyle_or_None, # solid
"grid.yaxis.minor.linewidth": validate_float_or_None, # in points< 628C /td>
"grid.yaxis.minor.alpha": validate_float_or_None,

Copy link
Member

Choose a reason for hiding this comment

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

Seeing this in code, it feels a bit too much. I've browsed though a lot of plots and didn't find relevant usage. If the grids are different on x and y, it's typically that only one of them is activated. I've not seen the case that people use different styles for x and y.

Therefore I revert my previous proposal and think we should leave this out (YAGNI). Sorry for the back-and-forth, but sometimes one has to go the step to see it was wrong. On the plus side, this interplay lead to the expansion of _val_or_rc() which I think is a real win.

Copy link
Author

Choose a reason for hiding this comment

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

That is far. I will revert the changes. It's simple anyway with the new _val_or_rc() to go back-and-forth.

## figure props
# figure title
"figure.titlesize": validate_fontsize,
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/rcsetup.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def validate_color_or_auto(s: Any) -> ColorType | Literal["auto"]: ...
def _validate_color_or_edge(s: Any) -> ColorType | Literal["edge"]: ...
def validate_color_for_prop_cycle(s: Any) -> ColorType: ...
def validate_color(s: Any) -> ColorType: ...
def validate_color_or_None(s: Any) -> ColorType | None: ...
def validate_colorlist(s: Any) -> list[ColorType]: ...
def _validate_color_or_linecolor(
s: Any,
Expand Down Expand Up @@ -139,7 +140,8 @@ def validate_fillstylelist(
s: Any,
) -> list[Literal["full", "left", "right", "bottom", "top", "none"]]: ...
def validate_markevery(s: Any) -> MarkEveryType: ...
def _validate_linestyle(s: Any) -> LineStyleType: ...
def _validate_linestyle(ls: Any) -> LineStyleType: ...
def _validate_linestyle_or_None(ls: Any) -> LineStyleType | None: ...
def validate_markeverylist(s: Any) -> list[MarkEveryType]: ...
def validate_bbox(s: Any) -> Literal["tight", "standard"] | None: ...
def validate_sketch(s: Any) -> None | tuple[float, float, float]: ...
Expand Down
Loading
0