Histogram Plot
Histograms of one or more value columns, optionally split by a group column.
Plots raw rows directly (no pre-aggregation). clip_range=(lower, upper) clamps out-of-range
values to the edge bins; matplotlib's native range kwarg drops them instead. A Series is
plotted as a single histogram; group_col cannot be used with one.
plot(df, value_col=None, group_col=None, title=None, eyebrow=None, subtitle=None, x_label=None, y_label=None, legend_title=None, ax=None, source_text=None, move_legend_outside=False, clip_range=None, use_hatch=False, **kwargs)
Plot a histogram of value_col, optionally split by group_col.
With group_col, one histogram is drawn per unique group value; a list
value_col cannot be combined with it. When df is a Series, value_col (if given)
becomes the single column name and the Series' own name is discarded; group_col
cannot be used with a Series and will error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Series
|
The dataframe (or series) to plot. |
required |
value_col
|
str or list of str
|
The column(s) to plot. |
None
|
group_col
|
str
|
The column used to define different histograms. |
None
|
title
|
str
|
The title of the plot. |
None
|
eyebrow
|
str
|
Small uppercase label rendered above the title. |
None
|
subtitle
|
str
|
Supporting copy rendered below the title. |
None
|
x_label
|
str
|
X-axis label; omitted means no label. |
None
|
y_label
|
str
|
Y-axis label; omitted means no label. |
None
|
legend_title
|
str
|
The title of the legend. |
None
|
ax
|
Axes
|
Matplotlib axes object to plot on. |
None
|
source_text
|
str
|
The source text to add to the plot. |
None
|
move_legend_outside
|
bool
|
Move the legend outside the plot. |
False
|
clip_range
|
tuple[float | None, float | None]
|
|
None
|
use_hatch
|
bool
|
Whether to use hatching for the bars. |
False
|
**kwargs
|
Any
|
Additional keyword arguments for pandas' |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
SubplotBase |
SubplotBase
|
The matplotlib axes object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both clip_range and matplotlib's range kwarg are specified. |
ValueError
|
If clip_range is not a 2-tuple or has lower greater than upper. |
ValueError
|
If value_col is a list and group_col is also provided. |
ValueError
|
If df is a DataFrame and value_col is None. |
Examples:
>>> from openretailscience.plots import histogram
>>> spend_per_customer = transactions.groupby("customer_id", as_index=False)["unit_spend"].sum()
>>> ax = histogram.plot(
... df=spend_per_customer, value_col="unit_spend", bins=10,
... eyebrow="Customers", title="Spend per customer", subtitle="January 2024 to June 2025",
... )
Source code in openretailscience/plots/histogram.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |