rcparams
rcparams
Package-wide defaults, in one validated mapping.
Every knob that used to live in a module-level DEFAULT_* dict is a key in
rcParams, the way matplotlib’s own rcParams works, and for the same
reason: a default that is worth having is worth being able to change once at
the top of a notebook instead of at every call site.
import mnutils
mnutils.rcParams["image.cmap"] = "viridis" # for the whole session
with mnutils.rc_context({"grid.panel_height": "30rem"}):
... # ...or just for a block
mnutils.rcdefaults() # back to the built-insThree rules make this safe to rely on:
Keys are validated on assignment.
_VALIDATORSdefines both the legal key set and what each key accepts, so a typo raises immediately (with a near-match suggestion) rather than being silently ignored until someone wonders why the colormap never changed.Nothing reads a default at import time. Functions take
Noneand resolve it in the body viaresolve_rc, so assigning torcParamsafterimport mnutilsactually takes effect. Binding a default in a signature (cmap=rcParams["image.cmap"]) freezes it at import and is the one mistake this module exists to prevent.An explicit argument always wins. Precedence is argument >
rc_contextrcParams> the built-in default in_DEFAULTS.
Attributes¶
| Name | Description |
|---|---|
rcParams | The live parameter mapping every MNUtils function reads its defaults from. |
rcParamsDefault | The built-in defaults. Never mutated -- rcdefaults restores from here. |
rc_presets |
Classes¶
| Name | Description |
|---|---|
| RcParams | A validated str -> value mapping of package defaults. |
RcParams¶
rcparams.RcParams(mapping=None)A validated str -> value mapping of package defaults.
Behaves like a dict, except that only keys in _VALIDATORS exist and every
assignment goes through that key’s validator. Keys cannot be added or
removed -- rcdefaults restores values, it never rebuilds the key set.
Methods¶
| Name | Description |
|---|---|
| copy | A detached RcParams holding the same values. |
| group | The keys under prefix, with the prefix stripped. |
copy¶
rcparams.RcParams.copy()A detached RcParams holding the same values.
group¶
rcparams.RcParams.group(prefix)The keys under prefix, with the prefix stripped.
rcParams.group("image.ax") gives {"xlabel": ..., "xticks": ...}
-- the shape a matplotlib.axes.Axes.set call wants, which is why the
axis defaults are stored as individual keys rather than as one opaque
dict value.
Functions¶
| Name | Description |
|---|---|
| rc_context | Temporarily override rcParams, restoring the previous values on exit. |
| rcdefaults | Restore every rcParam to its built-in default. |
| resolve_rc | Return value, or rcParams[key] when it is None. |
| validate_css_length | Accept "30rem"/"400px"/"60vh", or a bare number read as rem. |
rc_context¶
rcparams.rc_context(rc)Temporarily override rcParams, restoring the previous values on exit.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
| rc | dict | Overrides keyed by rcParam name, e.g. one of rc_presets. Names contain dots, so there is no keyword-argument form -- a mapping is the only way to spell them. | required |
Returns¶
| Name | Type | Description |
|---|---|---|
| RcParams | The live mapping, bound by as inside the block, so it can be inspected or further modified there. (NumPy convention would call this a Yields section; quartodoc’s renderer raises on one, so it is spelled Returns here.) |
Examples¶
>>> with rc_context({"image.cmap": "gray"}):
... passrcdefaults¶
rcparams.rcdefaults()Restore every rcParam to its built-in default.
resolve_rc¶
rcparams.resolve_rc(value, key)Return value, or rcParams[key] when it is None.
The one-liner behind every param: T | None = None signature in the
package. Resolving in the body rather than in the signature is what makes
an assignment to rcParams after import take effect at all.
validate_css_length¶
rcparams.validate_css_length(value)Accept "30rem"/"400px"/"60vh", or a bare number read as rem.
Public because call-site overrides (plotting.images.display_images’s
panel_height) bypass rcParams and so bypass its validation, while
ending up in the same style attribute.