Graph Utils
Helper functions for styling graphs.
apply_hatches(ax, num_segments)
Apply hatch patterns to patches in a plot, such as bars, histograms, or area plots.
This function divides the patches in the given Axes object into the specified number of segments and applies a different hatch pattern to each segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax |
Axes
|
The matplotlib Axes object containing the plot with patches (bars, histograms, etc.). |
required |
num_segments |
int
|
The number of segments to divide the patches into, with each segment receiving a different hatch pattern. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Axes |
Axes
|
The modified Axes object with hatches applied to the patches. |
Source code in openretailscience/plots/styles/graph_utils.py
draw_end_of_line_labels(ax)
Annotate each visible line with its series label at its right-most point.
Used by line-style plots when legend_style="end_of_line". For each
visible labeled line, places a small filled marker at the line's last
finite (x, y) and a colored text label to its right. When two or more
labels would overlap vertically, label y-positions are bumped apart by
the font line height (markers stay anchored at the true line endpoints)
and a thin leader connects each displaced label back to its marker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax |
Axes
|
Matplotlib axes containing the line plots. |
required |
Source code in openretailscience/plots/styles/graph_utils.py
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
format_shorthand(num, decimals=0, prefix='')
Format a number the way a person would write it, with K/M/B/T/P magnitude suffixes.
Examples:
500000 → "500K", 1.4e7 → "14M", 1500 → "2K" (zero decimals),
1500 → "1.5K" (one decimal). Trailing zeros are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num |
float
|
The number to format. |
required |
decimals |
int
|
The number of decimals. Defaults to 0. |
0
|
prefix |
str
|
The prefix of the returned string, eg '$'. Defaults to "". |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted number, with trailing zeros removed. |
Source code in openretailscience/plots/styles/graph_utils.py
get_decimals(axis_limits, tick_values, max_decimals=10)
Pick the smallest decimal count that keeps format_shorthand tick labels distinct.
Used by set_axis_shorthand when decimals are auto-derived for either the x-axis or the y-axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis_limits |
tuple[float, float]
|
The axis limits (xlim or ylim). |
required |
tick_values |
list[float]
|
The tick values on the same axis. |
required |
max_decimals |
int
|
The maximum number of decimals to use. Defaults to 10. |
10
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of decimals to use. |
Source code in openretailscience/plots/styles/graph_utils.py
set_axis_percent(fmt_axis, decimals=None, xmax=1, symbol='%')
Apply percent formatting to a matplotlib axis using matplotlib's PercentFormatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt_axis |
YAxis | XAxis
|
The axis to format (e.g. |
required |
decimals |
int | None
|
Number of decimal places. |
None
|
xmax |
float
|
The value that maps to 100%. Defaults to 1. |
1
|
symbol |
str | None
|
Symbol shown after the number; pass |
'%'
|
Source code in openretailscience/plots/styles/graph_utils.py
set_axis_shorthand(fmt_axis, decimals=None, prefix='')
Apply shorthand (K/M/B/T/P) numeric formatting to a matplotlib axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt_axis |
YAxis | XAxis
|
The axis to format (e.g. |
required |
decimals |
int | None
|
Number of decimal places. |
None
|
prefix |
str
|
Prepended to each formatted value (e.g. |
''
|
Source code in openretailscience/plots/styles/graph_utils.py
truncate_to_x_digits(num_str, digits)
Truncate a shorthand-formatted number to the first digits significant digits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_str |
str
|
The formatted number (e.g., '999.999K'). |
required |
digits |
int
|
The number of digits to keep. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The truncated formatted number (e.g., '999.9K'). |