From e4186b7dff7f26a808c6fb0f73e81eb2ea0c162c Mon Sep 17 00:00:00 2001 From: trananso Date: Mon, 6 May 2024 12:55:03 -0400 Subject: [PATCH 1/4] Support `auto` for `rcParams['axes.labelcolor']` --- lib/matplotlib/axis.py | 12 ++++++++++-- lib/matplotlib/mpl-data/matplotlibrc | 2 +- lib/matplotlib/rcsetup.py | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index d317f6ec0567..0f4719605902 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -658,11 +658,16 @@ def __init__(self, axes, *, pickradius=15, clear=True): self._autolabelpos = True + if not cbook._str_lower_equal(mpl.rcParams['axes.labelcolor'], 'auto'): + color = mpl.rcParams['axes.labelcolor'] + else: + color = mpl.rcParams['text.color'] + self.label = mtext.Text( np.nan, np.nan, fontsize=mpl.rcParams['axes.labelsize'], fontweight=mpl.rcParams['axes.labelweight'], - color=mpl.rcParams['axes.labelcolor'], + color=color ) self._set_artist_props(self.label) self.offsetText = mtext.Text(np.nan, np.nan) @@ -882,7 +887,10 @@ def clear(self): self.label._reset_visual_defaults() # The above resets the label formatting using text rcParams, # so we then update the formatting using axes rcParams - self.label.set_color(mpl.rcParams['axes.labelcolor']) + if not cbook._str_lower_equal(mpl.rcParams['axes.labelcolor'], 'auto'): + self.label.set_color(mpl.rcParams['axes.labelcolor']) + else: + self.label.set_color(mpl.rcParams['text.color']) self.label.set_fontsize(mpl.rcParams['axes.labelsize']) self.label.set_fontweight(mpl.rcParams['axes.labelweight']) self.offsetText._reset_visual_defaults() diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index 29ffb20f4280..6e7a22f96fd0 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -378,7 +378,7 @@ #axes.labelsize: medium # font size of the x and y labels #axes.labelpad: 4.0 # space between label and axis #axes.labelweight: normal # weight of the x and y labels -#axes.labelcolor: black +#axes.labelcolor: auto #axes.axisbelow: line # draw axis gridlines and ticks: # - below patches (True) # - above patches but below lines ('line') diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index b0cd22098489..666bcebbe806 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1101,7 +1101,7 @@ def _convert_validator_spec(key, conv): "axes.labelsize": validate_fontsize, # fontsize of x & y labels "axes.labelpad": validate_float, # space between label and axis "axes.labelweight": validate_fontweight, # fontsize of x & y labels - "axes.labelcolor": validate_color, # color of axis label + "axes.labelcolor": validate_color_or_auto, # color of axis label # use scientific notation if log10 of the axis range is smaller than the # first or larger than the second "axes.formatter.limits": _listify_validator(validate_int, n=2), From bc227254ce5ec51376afeef0fc991cce77f74de1 Mon Sep 17 00:00:00 2001 From: trananso Date: Mon, 13 May 2024 11:45:38 -0400 Subject: [PATCH 2/4] Default `x/ytick.labelcolor` to `text.color` --- lib/matplotlib/axis.py | 15 ++++++++------- lib/matplotlib/mpl-data/matplotlibrc | 4 ++-- lib/matplotlib/rcsetup.py | 10 ++++++++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 0f4719605902..842f0efee69d 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -129,9 +129,9 @@ def __init__( if labelcolor is None: labelcolor = mpl.rcParams[f"{name}.labelcolor"] - if cbook._str_equal(labelcolor, 'inherit'): - # inherit from tick color - labelcolor = mpl.rcParams[f"{name}.color"] + if (cbook._str_equal(labelcolor, 'inherit') or + cbook._str_equal(labelcolor, 'auto')): + labelcolor = mpl.rcParams["text.color"] if labelsize is None: labelsize = mpl.rcParams[f"{name}.labelsize"] @@ -2591,10 +2591,11 @@ def _init(self): ) self.label_position = 'left' - if mpl.rcParams['ytick.labelcolor'] == 'inherit': - tick_color = mpl.rcParams['ytick.color'] - else: - tick_color = mpl.rcParams['ytick.labelcolor'] + tick_color = mpl.rcParams['ytick.labelcolor'] + + if (cbook._str_equal(tick_color, 'inherit') or + cbook._str_equal(tick_color, 'auto')): + tick_color = mpl.rcParams["text.color"] # x in axes coords, y in display coords(!). self.offsetText.set( diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index 6e7a22f96fd0..04c0e8949fad 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -482,7 +482,7 @@ #xtick.major.pad: 3.5 # distance to major tick label in points #xtick.minor.pad: 3.4 # distance to the minor tick label in points #xtick.color: black # color of the ticks -#xtick.labelcolor: inherit # color of the tick labels or inherit from xtick.color +#xtick.labelcolor: auto # color of the tick labels or default to text.color #xtick.labelsize: medium # font size of the tick labels #xtick.direction: out # direction: {in, out, inout} #xtick.minor.visible: False # visibility of minor ticks on x-axis @@ -504,7 +504,7 @@ #ytick.major.pad: 3.5 # distance to major tick label in points #ytick.minor.pad: 3.4 # distance to the minor tick label in points #ytick.color: black # color of the ticks -#ytick.labelcolor: inherit # color of the tick labels or inherit from ytick.color +#ytick.labelcolor: auto # color of the tick labels or default to text.color #ytick.labelsize: medium # font size of the tick labels #ytick.direction: out # direction: {in, out, inout} #ytick.minor.visible: False # visibility of minor ticks on y-axis diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 666bcebbe806..1996a39133dd 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -301,6 +301,12 @@ def validate_color_or_auto(s): return validate_color(s) +def validate_color_inherit_or_auto(s): + if cbook._str_equal(s, 'inherit') or cbook._str_equal(s, 'auto'): + return s + return validate_color(s) + + def validate_color_for_prop_cycle(s): # N-th color cycle syntax can't go into the color cycle. if isinstance(s, str) and re.match("^C[0-9]$", s): @@ -1199,7 +1205,7 @@ def _convert_validator_spec(key, conv): "xtick.major.pad": validate_float, # distance to label in points "xtick.minor.pad": validate_float, # distance to label in points "xtick.color": validate_color, # color of xticks - "xtick.labelcolor": validate_color_or_inherit, # color of xtick labels + "xtick.labelcolor": validate_color_inherit_or_auto, # color of xtick labels "xtick.minor.visible": validate_bool, # visibility of minor xticks "xtick.minor.top": validate_bool, # draw top minor xticks "xtick.minor.bottom": validate_bool, # draw bottom minor xticks @@ -1222,7 +1228,7 @@ def _convert_validator_spec(key, conv): "ytick.major.pad": validate_float, # distance to label in points "ytick.minor.pad": validate_float, # distance to label in points "ytick.color": validate_color, # color of yticks - "ytick.labelcolor": validate_color_or_inherit, # color of ytick labels + "ytick.labelcolor": validate_color_inherit_or_auto, # color of ytick labels "ytick.minor.visible": validate_bool, # visibility of minor yticks "ytick.minor.left": validate_bool, # draw left minor yticks "ytick.minor.right": validate_bool, # draw right minor yticks From 189f44c40a7f07961c3e993953e89b862846c23d Mon Sep 17 00:00:00 2001 From: trananso Date: Mon, 13 May 2024 11:51:52 -0400 Subject: [PATCH 3/4] Default tick colors to the axes spine color --- lib/matplotlib/axis.py | 3 +++ lib/matplotlib/mpl-data/matplotlibrc | 4 ++-- lib/matplotlib/rcsetup.py | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 842f0efee69d..bb00d98cdc48 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -122,6 +122,9 @@ def __init__( if color is None: color = mpl.rcParams[f"{name}.color"] + if cbook._str_equal(color, 'auto'): + color = mpl.rcParams["axes.edgecolor"] + if pad is None: pad = mpl.rcParams[f"{name}.{major_minor}.pad"] self._base_pad = pad diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index 04c0e8949fad..f2f2112e3a79 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -481,7 +481,7 @@ #xtick.minor.width: 0.6 # minor tick width in points #xtick.major.pad: 3.5 # distance to major tick label in points #xtick.minor.pad: 3.4 # distance to the minor tick label in points -#xtick.color: black # color of the ticks +#xtick.color: auto # color of the ticks #xtick.labelcolor: auto # color of the tick labels or default to text.color #xtick.labelsize: medium # font size of the tick labels #xtick.direction: out # direction: {in, out, inout} @@ -503,7 +503,7 @@ #ytick.minor.width: 0.6 # minor tick width in points #ytick.major.pad: 3.5 # distance to major tick label in points #ytick.minor.pad: 3.4 # distance to the minor tick label in points -#ytick.color: black # color of the ticks +#ytick.color: auto # color of the ticks #ytick.labelcolor: auto # color of the tick labels or default to text.color #ytick.labelsize: medium # font size of the tick labels #ytick.direction: out # direction: {in, out, inout} diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 1996a39133dd..69cc8ac8243e 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1204,7 +1204,7 @@ def _convert_validator_spec(key, conv): "xtick.minor.width": validate_float, # minor xtick width in points "xtick.major.pad": validate_float, # distance to label in points "xtick.minor.pad": validate_float, # distance to label in points - "xtick.color": validate_color, # color of xticks + "xtick.color": validate_color_or_auto, # color of xticks "xtick.labelcolor": validate_color_inherit_or_auto, # color of xtick labels "xtick.minor.visible": validate_bool, # visibility of minor xticks "xtick.minor.top": validate_bool, # draw top minor xticks @@ -1227,7 +1227,7 @@ def _convert_validator_spec(key, conv): "ytick.minor.width": validate_float, # minor ytick width in points "ytick.major.pad": validate_float, # distance to label in points "ytick.minor.pad": validate_float, # distance to label in points - "ytick.color": validate_color, # color of yticks + "ytick.color": validate_color_or_auto, # color of yticks "ytick.labelcolor": validate_color_inherit_or_auto, # color of ytick labels "ytick.minor.visible": validate_bool, # visibility of minor yticks "ytick.minor.left": validate_bool, # draw left minor yticks From bdbc0f922cd5edaadeaed39ec66097509459449b Mon Sep 17 00:00:00 2001 From: trananso Date: Mon, 13 May 2024 16:41:37 -0400 Subject: [PATCH 4/4] Add missing stub for `validate_color_inherit_or_auto` --- lib/matplotlib/rcsetup.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/rcsetup.pyi b/lib/matplotlib/rcsetup.pyi index 1538dac7510e..9846638a1016 100644 --- a/lib/matplotlib/rcsetup.pyi +++ b/lib/matplotlib/rcsetup.pyi @@ -46,8 +46,9 @@ def validate_fonttype(s: Any) -> int: ... _auto_backend_sentinel: object def validate_backend(s: Any) -> str: ... -def validate_color_or_inherit(s: Any) -> Literal["inherit"] | ColorType: ... +def validate_color_or_inherit(s: Any) -> ColorType | Literal["inherit"]: ... def validate_color_or_auto(s: Any) -> ColorType | Literal["auto"]: ... +def validate_color_inherit_or_auto(s: Any) -> ColorType | Literal["inherit"] | Literal["auto"]: ... def validate_color_for_prop_cycle(s: Any) -> ColorType: ... def validate_color(s: Any) -> ColorType: ... def validate_colorlist(s: Any) -> list[ColorType]: ...