explore_bar_plot_by_group

source

explore_bar_plot_by_group(
    df,
    by_var,
    var,
    stat_fun=np.nanmean,
    *,
    order_by_stat=False,
    color=None,
    title=None,
    subtitle=None,
)

Bar chart of a statistic of var computed within each by_var group.

Parameters

Name Type Description Default
df pd.DataFrame Data frame containing the grouping factor and the numeric variable. required
by_var str Grouping column. required
var str Numeric column to summarise. required
stat_fun Callable[[np.ndarray], float] Statistic applied to the non-missing values of each group. Defaults to :func:numpy.nanmean. Missing values are dropped before the call, matching R’s na.rm = TRUE. np.nanmean
order_by_stat bool If True, bars are ordered by the statistic (largest at the top); otherwise the groups keep their order of appearance. False
color str | None Bar fill color. Defaults to the primary theme color. None

Returns

Name Type Description
ByGroupBarGraphResult df (columns by_var and stat_<var>) and the Plotly fig.

Examples

Basic — mean of a variable within each group:

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_bar_plot_by_group(df, "continent", "gini_regional").fig

Advanced — a different statistic, bars ordered by it, a custom color, and the per-group values from .df:

import numpy as np
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)
result = ex.explore_bar_plot_by_group(
    df, "continent", "gini_regional",
    stat_fun=np.nanmedian, order_by_stat=True, color="#4682b4",
)
result.fig
result.df
continent stat_gini_regional
0 Continent A 0.290929
1 Continent B 0.286590
2 Continent C 0.263102
4 Continent E 0.254482
3 Continent D 0.252084