Heuristic:DistrictDataLabs Yellowbrick Matplotlib rcParams Management
| Knowledge Sources | |
|---|---|
| Domains | Visualization, Styling |
| Last Updated | 2026-02-08 05:00 GMT |
Overview
Yellowbrick automatically modifies matplotlib rcParams on import, providing consistent visual aesthetics while preserving the ability to reset to user or default settings.
Description
When `import yellowbrick` is executed, the library immediately calls `set_aesthetic()` which modifies `matplotlib.rcParams` to apply Yellowbrick's custom color palette, font settings, grid style, and figure sizing. This is modeled after Seaborn's `rcmod.py` approach. To avoid permanently overwriting user settings, the original `rcParams` are captured before any modification and stored in `_orig_rc_params`. Users can call `reset_orig()` to restore their pre-import settings or `reset_defaults()` to restore matplotlib's factory defaults.
The styling system is version-aware: it checks for matplotlib >= 1.5.0 to decide whether to use the modern `axes.prop_cycle` (via cycler) or the legacy `axes.color_cycle` parameter, and includes a specific workaround for a matplotlib 1.4.2 bug where points become invisible.
Usage
Be aware that importing Yellowbrick changes matplotlib's global state. If you have custom matplotlib styling that needs to be preserved, call `yellowbrick.reset_orig()` after import. If you need Yellowbrick's aesthetics only temporarily, use the style context managers (`_AxesStyle`, `_PlottingContext`) as decorators or with-statements.
The Insight (Rule of Thumb)
- Action: Capture `mpl.rcParams.copy()` before any modification. Call `set_aesthetic()` at module import for zero-configuration styling. Provide `reset_orig()` and `reset_defaults()` escape hatches.
- Value: Default figure size is `[8, 5.5]`, font size 12, with a custom "yellowbrick" color palette. Grid is enabled by default with light gray lines.
- Trade-off: Importing Yellowbrick modifies global matplotlib state, which may surprise users who import it alongside other visualization libraries. The `reset_orig()` function mitigates this.
- Version awareness: Check `mpl.__version__` for color cycle API (`prop_cycle` vs `color_cycle`) and apply specific workarounds for known matplotlib bugs.
Reasoning
Consistent, publication-quality visualizations are a core value proposition of Yellowbrick. By applying aesthetics on import (just like Seaborn), every visualizer produces styled output without requiring users to configure matplotlib manually. The capture-modify-restore pattern is a well-established approach in the matplotlib ecosystem that balances convenience with user control.
The matplotlib 1.4.2 workaround (`lines.markeredgewidth = 0.01`) demonstrates the kind of tribal knowledge that accumulates in visualization libraries: a specific bug where points with `markeredgewidth = 0` are rendered invisible. This was identified via the Seaborn project (mwaskom/seaborn#344) and carried over to Yellowbrick.
Code Evidence
Original rcParams capture from `yellowbrick/__init__.py:24-25`:
import matplotlib as mpl
_orig_rc_params = mpl.rcParams.copy()
Automatic aesthetic application from `yellowbrick/__init__.py:49`:
set_aesthetic() # NOTE: modifies mpl.rcParams
Version-aware color cycle from `yellowbrick/style/rcmod.py:431-439`:
if mpl_ge_150:
from cycler import cycler
cyl = cycler("color", colors)
mpl.rcParams["axes.prop_cycle"] = cyl
else:
mpl.rcParams["axes.color_cycle"] = list(colors)
Matplotlib 1.4.2 bug workaround from `yellowbrick/style/rcmod.py:329-334`:
# Implement hack workaround for matplotlib bug
# See https://github.com/mwaskom/seaborn/issues/344
if mpl.__version__ == "1.4.2":
context_dict["lines.markeredgewidth"] = 0.01
Reset functions from `yellowbrick/style/rcmod.py:135-146`:
def reset_defaults():
"""Restore all RC params to default settings."""
mpl.rcParams.update(mpl.rcParamsDefault)
def reset_orig():
"""Restore all RC params to original settings (respects custom rc)."""
mpl.rcParams.update(_orig_rc_params)