Skip to content

Regularization and mixed-model plots

This page documents 4 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.

Conceptual guide

plot_gwlasso_selection_frequency

Plot the percentage of locations with a non-zero coefficient.

Property Value
Type function
Import from pygwrx.plotting import plot_gwlasso_selection_frequency
Signature plot_gwlasso_selection_frequency(model, *, theme: 'str' = 'default', ax: 'Optional[plt.Axes]' = None, figsize: 'Optional[Tuple[float, float]]' = None, title: 'str' = 'GWLasso local variable selection')
Maintained example examples/plotting/03_robust_regularized_bootstrap.py

plot_gwlasso_selection_frequency

plot_gwlasso_selection_frequency(
    model,
    *,
    theme: str = "default",
    ax: Optional[Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: str = "GWLasso local variable selection"
)

Plot the percentage of locations with a non-zero coefficient.

Source code in src/pygwrx/plotting/regularization.py
def plot_gwlasso_selection_frequency(
    model,
    *,
    theme: str = "default",
    ax: Optional[plt.Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: str = "GWLasso local variable selection",
):
    """Plot the percentage of locations with a non-zero coefficient."""
    coef = np.asarray(getattr(model, "coef_", None), dtype=float)
    if coef.ndim != 2:
        raise ValueError("The fitted model does not expose a local coef_ matrix.")
    active_tol = float(getattr(model, "active_tol", 1.0e-8))
    frequency = np.mean(np.abs(coef) > active_tol, axis=0) * 100.0
    names = list(feature_names(model))
    with plotting_theme(theme):
        fig, axis = figure_axis(ax, figsize, theme, wide=True)
        axis.bar(names, frequency)
        axis.set_ylim(0.0, 100.0)
        axis.set_ylabel("Selected locations (%)")
        axis.set_title(title)
        axis.tick_params(axis="x", rotation=30)
        axis.grid(True, axis="y", alpha=0.22)
        fig.tight_layout()
        return fig, axis

plot_gwlasso_active_map

Map locations where a GWLasso coefficient is active.

Property Value
Type function
Import from pygwrx.plotting import plot_gwlasso_active_map
Signature plot_gwlasso_active_map(model, feature, *, geometry=None, theme: 'str' = 'default', ax: 'Optional[plt.Axes]' = None, figsize: 'Optional[Tuple[float, float]]' = None, title: 'Optional[str]' = None)
Maintained example examples/plotting/03_robust_regularized_bootstrap.py

plot_gwlasso_active_map

plot_gwlasso_active_map(
    model,
    feature,
    *,
    geometry=None,
    theme: str = "default",
    ax: Optional[Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: Optional[str] = None
)

Map locations where a GWLasso coefficient is active.

Source code in src/pygwrx/plotting/regularization.py
def plot_gwlasso_active_map(
    model,
    feature,
    *,
    geometry=None,
    theme: str = "default",
    ax: Optional[plt.Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: Optional[str] = None,
):
    """Map locations where a GWLasso coefficient is active."""
    names = list(feature_names(model))
    if isinstance(feature, str):
        if feature not in names:
            raise ValueError(f"Unknown feature {feature!r}.")
        index = names.index(feature)
    else:
        index = int(feature)
    coef = np.asarray(getattr(model, "coef_", None), dtype=float)
    if coef.ndim != 2 or index < 0 or index >= coef.shape[1]:
        raise ValueError("feature is outside the fitted coefficient range.")
    active = (
        np.abs(coef[:, index]) > float(getattr(model, "active_tol", 1.0e-8))
    ).astype(float)
    with plotting_theme(theme):
        fig, axis = figure_axis(ax, figsize, theme)
        cmap, norm = resolve_color_scale(
            active, center_zero=False, vmin=-0.01, vmax=1.01, cmap="Greys"
        )
        render_spatial_values(
            axis,
            active,
            coords=coords_for_model(model),
            geometry=geometry,
            cmap=cmap,
            norm=norm,
        )
        axis.set_title(title or f"GWLasso active coefficient: {names[index]}")
        fig.tight_layout()
        return fig, axis

plot_gwlasso_alpha

Map the locally selected Lasso penalty.

Property Value
Type function
Import from pygwrx.plotting import plot_gwlasso_alpha
Signature plot_gwlasso_alpha(model, *, geometry=None, theme: 'str' = 'default', ax: 'Optional[plt.Axes]' = None, figsize: 'Optional[Tuple[float, float]]' = None, title: 'str' = 'GWLasso local regularization')
Maintained example examples/plotting/03_robust_regularized_bootstrap.py

plot_gwlasso_alpha

plot_gwlasso_alpha(
    model,
    *,
    geometry=None,
    theme: str = "default",
    ax: Optional[Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: str = "GWLasso local regularization"
)

Map the locally selected Lasso penalty.

Source code in src/pygwrx/plotting/regularization.py
def plot_gwlasso_alpha(
    model,
    *,
    geometry=None,
    theme: str = "default",
    ax: Optional[plt.Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: str = "GWLasso local regularization",
):
    """Map the locally selected Lasso penalty."""
    alpha = np.asarray(getattr(model, "alpha_", None), dtype=float)
    if alpha.ndim == 0:
        alpha = np.full(coords_for_model(model).shape[0], float(alpha))
    alpha = alpha.reshape(-1)
    with plotting_theme(theme):
        fig, axis = figure_axis(ax, figsize, theme)
        cmap, norm = resolve_color_scale(alpha, center_zero=False, cmap="magma")
        artist = render_spatial_values(
            axis,
            alpha,
            coords=coords_for_model(model),
            geometry=geometry,
            cmap=cmap,
            norm=norm,
        )
        fig.colorbar(artist, ax=axis, fraction=0.046, pad=0.04, label="Alpha")
        axis.set_title(title)
        fig.tight_layout()
        return fig, axis

plot_mixed_gwr_coefficients

Compare global coefficients with distributions of local coefficients.

Property Value
Type function
Import from pygwrx.plotting import plot_mixed_gwr_coefficients
Signature plot_mixed_gwr_coefficients(model, *, theme: 'str' = 'default', figsize: 'Optional[Tuple[float, float]]' = None, title: 'str' = 'Mixed GWR global and local coefficients')
Maintained example examples/plotting/03_robust_regularized_bootstrap.py

plot_mixed_gwr_coefficients

plot_mixed_gwr_coefficients(
    model,
    *,
    theme: str = "default",
    figsize: Optional[Tuple[float, float]] = None,
    title: str = "Mixed GWR global and local coefficients"
)

Compare global coefficients with distributions of local coefficients.

Source code in src/pygwrx/plotting/regularization.py
def plot_mixed_gwr_coefficients(
    model,
    *,
    theme: str = "default",
    figsize: Optional[Tuple[float, float]] = None,
    title: str = "Mixed GWR global and local coefficients",
):
    """Compare global coefficients with distributions of local coefficients."""
    global_coef = np.asarray(getattr(model, "coef_global_", None), dtype=float).reshape(
        -1
    )
    local_coef = np.asarray(getattr(model, "coef_local_", None), dtype=float)
    if local_coef.ndim != 2:
        raise ValueError("The fitted model does not expose coef_local_.")
    names = list(feature_names(model))
    global_indices = np.asarray(getattr(model, "global_var_indices_", []), dtype=int)
    local_indices = np.asarray(getattr(model, "local_var_indices_", []), dtype=int)
    with plotting_theme(theme):
        fig, axes = plt.subplots(1, 2, figsize=figsize or (11.0, 4.5))
        if global_coef.size:
            axes[0].bar([names[i] for i in global_indices], global_coef)
            axes[0].axhline(0.0, color="0.4", linestyle="--", linewidth=0.8)
        else:
            axes[0].text(0.5, 0.5, "No global variables", ha="center", va="center")
        axes[0].set_title("Global coefficients")
        if local_coef.shape[1]:
            labels = [names[i] for i in local_indices]
            version_parts = matplotlib.__version__.split(".")
            version = (int(version_parts[0]), int(version_parts[1]))
            label_keyword = "tick_labels" if version >= (3, 9) else "labels"
            boxplot_kwargs = {label_keyword: labels}
            axes[1].boxplot(
                [local_coef[:, j] for j in range(local_coef.shape[1])],
                showfliers=False,
                **boxplot_kwargs,
            )
            axes[1].axhline(0.0, color="0.4", linestyle="--", linewidth=0.8)
        axes[1].set_title("Local coefficient distributions")
        for axis in axes:
            axis.tick_params(axis="x", rotation=30)
            axis.grid(True, axis="y", alpha=0.22)
        fig.suptitle(title)
        fig.tight_layout()
        return fig, axes

Runnable examples used on this page

examples/plotting/03_robust_regularized_bootstrap.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT

"""All robust, GLM, Lasso, mixed, bootstrap, and scalable plots."""

# Allow this script to run directly from any working directory.
import sys
from pathlib import Path

_PROJECT_ROOT = Path(__file__).resolve().parents[2]
_EXAMPLES_ROOT = _PROJECT_ROOT / "examples"
_SRC_ROOT = _PROJECT_ROOT / "src"
for _path in (_SRC_ROOT, _EXAMPLES_ROOT):
    if str(_path) not in sys.path:
        sys.path.insert(0, str(_path))

import matplotlib

matplotlib.use("Agg", force=True)
from _common import save_plot
from _models import regularized_models

from pygwrx.plotting import (
    plot_bootstrap_bandwidths,
    plot_bootstrap_pvalues,
    plot_gwglm_residuals,
    plot_gwlasso_active_map,
    plot_gwlasso_alpha,
    plot_gwlasso_selection_frequency,
    plot_mixed_gwr_coefficients,
    plot_rgwr_convergence,
    plot_rgwr_weights,
    plot_scalable_gwr_kernel,
)

X, y, coords, rgwr, gwglm, gwlasso, mixed, bootstrap, scalable = regularized_models()
plots = {
    "rgwr_weights.png": plot_rgwr_weights(rgwr),
    "rgwr_convergence.png": plot_rgwr_convergence(rgwr),
    "gwglm_residuals.png": plot_gwglm_residuals(gwglm),
    "gwlasso_frequency.png": plot_gwlasso_selection_frequency(gwlasso),
    "gwlasso_active.png": plot_gwlasso_active_map(gwlasso, "x1"),
    "gwlasso_alpha.png": plot_gwlasso_alpha(gwlasso),
    "mixed_coefficients.png": plot_mixed_gwr_coefficients(mixed),
    "bootstrap_pvalues.png": plot_bootstrap_pvalues(bootstrap, "x1"),
    "bootstrap_bandwidths.png": plot_bootstrap_bandwidths(bootstrap),
    "scalable_kernel.png": plot_scalable_gwr_kernel(scalable),
}
for name, result in plots.items():
    print(save_plot(result, name))