Bar Plot
Bar and grouped bar plots from a DataFrame or Series.
Grouped bars come from a list of value_col columns; x_col only sets the x-axis categories.
A Series is plotted against its index — x_col cannot be combined with one. Use index.plot
for relative performance against a baseline and line.plot for sequential x-values.
plot(df, value_col=None, x_col=None, title=None, eyebrow=None, subtitle=None, x_label=None, y_label=None, legend_title=None, legend_labels=None, ax=None, source_text=None, move_legend_outside=False, orientation='vertical', sort_order=None, data_label_format=None, use_hatch=False, num_digits=3, **kwargs)
Create a bar plot (vertical or horizontal) from a DataFrame or Series.
A list of value_col columns creates grouped bars; x_col is the x-axis category
column. When df is a Series, it is plotted against its index, value_col may be None,
and x_col must be None. For a DataFrame, value_col=None becomes ["Value"] and
raises KeyError unless a column literally named "Value" exists. A UserWarning is
emitted when a percentage data_label_format is combined with negative values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Series
|
The input DataFrame or Series containing the data to be plotted. |
required |
value_col
|
str | list[str]
|
The column(s) containing values to plot as bars. Multiple columns create grouped bars. |
None
|
x_col
|
str
|
The x-axis category column (e.g., products, regions). |
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 for the legend. |
None
|
legend_labels
|
list[str]
|
Override the legend labels read from the plotted series
(e.g. swap column ids for human-readable names). Length must match the number of legend
entries or a |
None
|
ax
|
Axes
|
The Matplotlib Axes object to plot on. |
None
|
source_text
|
str
|
Text to be displayed as a source at the bottom of the plot. |
None
|
move_legend_outside
|
bool
|
Whether to move the legend outside the plot area. |
False
|
orientation
|
Literal['horizontal', 'h', 'vertical', 'v']
|
Orientation of the bars. Accepts short or long forms, case-insensitive. |
'vertical'
|
sort_order
|
Literal['asc', 'ascending', 'desc', 'descending'] | None
|
Sorting order
for the bars, by the first |
None
|
data_label_format
|
Literal['absolute', 'percentage_by_bar_group', 'percentage_by_series'] | None
|
Format for the data labels. "absolute" shows the raw value of each bar; "percentage_by_bar_group" shows each bar's share of its x-axis group; "percentage_by_series" shows each bar's share of its value-column series. |
None
|
use_hatch
|
bool
|
Whether to apply hatch patterns to the bars. |
False
|
num_digits
|
int
|
The number of digits to display in the data labels. |
3
|
**kwargs
|
Any
|
Additional keyword arguments for pandas' |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
SubplotBase |
SubplotBase
|
The Matplotlib Axes object with the generated plot. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If df is empty. |
ValueError
|
If df is a Series and x_col is provided. |
ValueError
|
If orientation, sort_order, or data_label_format is not an accepted value. |
ValueError
|
If legend_labels is provided and its length does not match the number of legend entries. |
Source code in openretailscience/plots/bar.py
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |