Skip to content

Refactor kde_distributions and extract density overlay logic into plot utilities#110

Merged
Oscar-Gil-Data merged 2 commits into
mainfrom
plot_utils_LS
Dec 13, 2025
Merged

Refactor kde_distributions and extract density overlay logic into plot utilities#110
Oscar-Gil-Data merged 2 commits into
mainfrom
plot_utils_LS

Conversation

@lshpaner
Copy link
Copy Markdown
Collaborator

Refactoring the existing kde_distributions plotting function to improve clarity, extensibility, and internal structure. Density overlay logic (KDE and parametric PDFs) has been extracted into a dedicated utility function housed in a new _plot_utils.py module.

Key Changes

  • Extracted density overlay logic
    Moved all KDE and parametric density plotting into _plot_density_overlays, a reusable internal helper that enforces a single, consistent color pipeline for all density curves.

  • Decoupled orchestration from rendering
    kde_distributions now focuses on high-level plotting flow (grid layout, labeling, legends, saving), while _plot_density_overlays handles low-level density rendering.

  • Expanded density support beyond KDE
    Density overlays now support any valid scipy.stats distribution (e.g., norm, lognorm, gamma) in addition to KDE, with explicit validation and clear error messaging for invalid inputs.

    Supported options include:

    kde,
    alpha, anglit, arcsine, argus, beta, betaprime, bradford, burr, burr12, cauchy,
    chi, chi2, cosine, crystalball, dgamma, dpareto_lognorm, dweibull, erlang,
    expon, exponnorm, exponpow, exponweib, f, fatiguelife, fisk, foldcauchy,
    foldnorm, gamma, gausshyper, genexpon, genextreme, gengamma,
    genhalflogistic, genhyperbolic, geninvgauss, genlogistic, gennorm,
    genpareto, gibrat, gompertz, gumbel_l, gumbel_r, halfcauchy,
    halfgennorm, halflogistic, halfnorm, hypsecant, invgamma, invgauss,
    invweibull, irwinhall, jf_skew_t, johnsonsb, johnsonsu, kappa3, kappa4,
    ksone, kstwo, kstwobign, landau, laplace, laplace_asymmetric, levy, levy_l,
    levy_stable, loggamma, logistic, loglaplace, lognorm, loguniform, lomax,
    maxwell, mielke, moyal, multivariate_normal, nakagami, ncf, nct, ncx2,
    norm, norminvgauss, pareto, pearson3, powerlaw, powerlognorm, powernorm,
    rayleigh, rdist, recipinvgauss, reciprocal, rel_breitwigner, rice,
    rv_continuous, rv_histogram, semicircular, skewcauchy, skewnorm,
    studentized_range, t, trapezoid, triang, truncexpon, truncnorm,
    truncpareto, truncweibull_min, tukeylambda, uniform, vonmises,
    vonmises_fisher, vonmises_line, wald, weibull_max, weibull_min, wrapcauchy.

  • Improved API semantics

    • Renamed color handling to density_color for clarity and consistency.
    • plot_type no longer exposes "kde" as a top-level mode; KDE is treated as one possible density function.
    • Invalid density_function values raise explicit, readable errors.
  • Internal utilities module introduced
    Added _plot_utils.py to house reusable plotting helpers, reducing duplication and keeping plots.py focused on public-facing API behavior.

Backward Compatibility

  • Existing workflows remain supported.
  • The kde_distributions interface is preserved (and may be aliased if renamed in a follow-up PR).

Why this matters

This refactor improves maintainability, reduces internal complexity, and makes it easier to extend distribution plotting without bloating the main plotting API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants