Skip to content

Robust and GLM plots

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

Conceptual guide

plot_rgwr_weights

Map final robust weights and outline completely rejected observations.

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

plot_rgwr_weights

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

Map final robust weights and outline completely rejected observations.

Source code in src/pygwrx/plotting/robust.py
def plot_rgwr_weights(
    model,
    *,
    geometry=None,
    theme: str = "default",
    ax: Optional[plt.Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    cmap: str = "viridis",
    title: Optional[str] = None,
):
    """Map final robust weights and outline completely rejected observations."""
    frame = local_diagnostic_frame(model)
    if "robust_weight" not in frame:
        raise ValueError("The fitted model does not expose robust_weights_.")
    values = frame["robust_weight"].to_numpy(float)
    with plotting_theme(theme):
        fig, axis = figure_axis(ax, figsize, theme)
        cmap_name, norm = resolve_color_scale(values, center_zero=False, cmap=cmap)
        artist = render_spatial_values(
            axis,
            values,
            coords=coords_for_model(model),
            geometry=geometry,
            cmap=cmap_name,
            norm=norm,
        )
        add_colorbar(fig, axis, artist, "Robust weight")
        rejected = values <= np.finfo(float).eps
        if geometry is None and np.any(rejected):
            coords = coords_for_model(model)
            axis.scatter(
                coords[rejected, 0],
                coords[rejected, 1],
                facecolors="none",
                edgecolors="black",
                s=95,
                linewidths=1.2,
                label="Rejected",
            )
            axis.legend(loc="best")
        axis.set_title(title or f"{model.__class__.__name__}: robust weights")
        fig.tight_layout()
        return fig, axis

plot_rgwr_convergence

Plot iteration MSE and the number of downweighted observations.

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

plot_rgwr_convergence

plot_rgwr_convergence(
    model,
    *,
    theme: str = "default",
    ax: Optional[Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: str = "Robust GWR convergence"
)

Plot iteration MSE and the number of downweighted observations.

Source code in src/pygwrx/plotting/robust.py
def plot_rgwr_convergence(
    model,
    *,
    theme: str = "default",
    ax: Optional[plt.Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: str = "Robust GWR convergence",
):
    """Plot iteration MSE and the number of downweighted observations."""
    mse = np.asarray(getattr(model, "mse_history_", None), dtype=float).reshape(-1)
    history = getattr(model, "weight_history_", None)
    if mse.size == 0 or not np.all(np.isfinite(mse)):
        raise ValueError("The fitted model does not expose a finite mse_history_.")
    with plotting_theme(theme):
        fig, axis = figure_axis(ax, figsize, theme, wide=True)
        iterations = np.arange(mse.size)
        axis.plot(iterations, mse, marker="o", label="MSE")
        axis.set_xlabel("Iteration")
        axis.set_ylabel("Mean squared error")
        axis.set_title(title)
        axis.grid(True, alpha=0.22)
        if history is not None:
            down = np.asarray(
                [
                    np.sum(np.asarray(weights, dtype=float) < 1.0 - 1.0e-12)
                    for weights in history
                ],
                dtype=float,
            )
            if down.size == mse.size:
                second = axis.twinx()
                second.plot(
                    iterations, down, linestyle="--", marker="s", label="Downweighted"
                )
                second.set_ylabel("Downweighted observations")
                lines = axis.get_lines() + second.get_lines()
                axis.legend(lines, [line.get_label() for line in lines], loc="best")
        fig.tight_layout()
        return fig, axis

plot_gwglm_residuals

Map Pearson, deviance, or raw residuals from a fitted GWGLM.

Property Value
Type function
Import from pygwrx.plotting import plot_gwglm_residuals
Signature plot_gwglm_residuals(model, *, residual: 'str' = 'deviance', 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_gwglm_residuals

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

Map Pearson, deviance, or raw residuals from a fitted GWGLM.

Source code in src/pygwrx/plotting/robust.py
def plot_gwglm_residuals(
    model,
    *,
    residual: str = "deviance",
    geometry=None,
    theme: str = "default",
    ax: Optional[plt.Axes] = None,
    figsize: Optional[Tuple[float, float]] = None,
    title: Optional[str] = None,
):
    """Map Pearson, deviance, or raw residuals from a fitted GWGLM."""
    token = str(residual).strip().lower()
    attributes = {
        "deviance": ("deviance_residuals_", "Deviance residual"),
        "pearson": ("pearson_residuals_", "Pearson residual"),
        "raw": ("residuals_", "Raw residual"),
    }
    if token not in attributes:
        raise ValueError("residual must be 'deviance', 'pearson', or 'raw'.")
    attribute, label = attributes[token]
    values = getattr(model, attribute, None)
    if values is None:
        raise ValueError(f"The fitted model does not expose {attribute}.")
    array = np.asarray(values, dtype=float).reshape(-1)
    with plotting_theme(theme):
        fig, axis = figure_axis(ax, figsize, theme)
        cmap, norm = resolve_color_scale(array, center_zero=True)
        artist = render_spatial_values(
            axis,
            array,
            coords=coords_for_model(model),
            geometry=geometry,
            cmap=cmap,
            norm=norm,
        )
        add_colorbar(fig, axis, artist, label)
        family = getattr(model, "family", "GWGLM")
        axis.set_title(title or f"{model.__class__.__name__} ({family}): {label}")
        fig.tight_layout()
        return fig, axis

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))