explore_histogram

source

explore_histogram(
    df,
    var=None,
    *,
    bins=30,
    kde=False,
    normal=False,
    title=None,
    subtitle=None,
)

Histogram of a numeric variable, optionally with density overlays.

Parameters

Name Type Description Default
df pd.DataFrame Data frame containing var. required
var str | None Numeric column to bin. Defaults to the declared main outcome (:func:expdpy.set_roles) when omitted. None
bins int Number of equal-width bins. 30
kde bool Overlay a Gaussian kernel-density estimate of the distribution. False
normal bool Overlay a normal curve with the sample mean and standard deviation. False

Returns

Name Type Description
HistogramResult df (columns bin_left, bin_right, count) and the Plotly fig.

Notes

The density overlays are drawn on the Density scale, so requesting either one opens the figure in Density view; the built-in Count/Density toggle hides the overlays in Count view and shows them in Density view.

Examples

Basic — a 30-bin histogram of a numeric variable, with readable labels from the data dictionary:

import expdpy as ex
from expdpy.data import load_kuznets, load_kuznets_data_def

df = ex.set_labels(load_kuznets(), load_kuznets_data_def(), set_panel=True)
ex.explore_histogram(df, "gini_regional").fig

Advanced — overlay a kernel-density estimate and a normal curve:

import expdpy as ex
from expdpy.data import load_kuznets, load_kuznets_data_def

df = ex.set_labels(load_kuznets(), load_kuznets_data_def(), set_panel=True)
ex.explore_histogram(df, "log_gdp_pc", bins=40, kde=True, normal=True).fig