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.
Splits ax.patches into num_segments groups and applies one hatch pattern per group,
mirroring the hatches onto the legend patches.
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. |
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.
Entry point for legend_style="end_of_line". For each visible labeled line, places a
marker at the line's last finite (x, y) and a colored label to its right; overlapping
labels are bumped apart by the font line height (markers stay anchored) 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
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 262 263 264 265 266 267 268 269 | |
expand_ylim_for_bar_labels(ax, labels)
Grow the y-limits so bar-end value labels sit inside the axes data area.
Matplotlib leaves no headroom for ax.bar_label labels at the bar ends, so they can spill
past the axes; the room added here scales with the rendered label size. Call it after
apply_chart_chrome (it measures the axes at their final size); it redraws the figure
up to 3 times. Emits UserWarning if the labels still overflow, e.g. on a very short axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
The axes holding the labelled bars. |
required |
labels
|
list[Annotation]
|
The annotation artists returned by |
required |
Source code in openretailscience/plots/styles/graph_utils.py
format_shorthand(num, decimals=0, prefix='')
Format a number with K/M/B/T/P magnitude suffixes, dropping trailing zeros.
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'). |