Graph Utils
Helper functions for styling graphs.
add_regression_line(ax, regression_type='linear', color='red', linestyle='--', text_position=0.6, show_equation=True, show_r2=True, **kwargs)
Add a regression line with configurable algorithm to a matplotlib plot.
This function examines the data in a matplotlib Axes object and adds a regression line to it. It supports line plots, scatter plots, and bar charts (both vertical and horizontal), and can handle both numeric and datetime x-axis values.
For bar charts, the function automatically detects orientation using matplotlib's BarContainer API and extracts appropriate x,y coordinates from bar positions and heights.
Note: If an axes contains multiple plot types (e.g., both lines and bars), the function processes them in priority order: lines first, then bars, then scatter plots. Only the first available plot type will be used for regression analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax |
Axes
|
The matplotlib axes object containing the plot (line, scatter, or bar). |
required |
regression_type |
Literal['linear', 'power', 'logarithmic', 'exponential']
|
Regression algorithm to use. Supported values: - "linear": y = mx + b (default, OLS regression) - "power": y = ax^b (elasticity analysis, log-log transformation) - "logarithmic": y = a*ln(x) + b (diminishing returns analysis) - "exponential": y = ae^(bx) (growth/decay patterns) Defaults to "linear". |
'linear'
|
color |
str
|
Color of the regression line. Defaults to "red". |
'red'
|
linestyle |
str
|
Style of the regression line. Defaults to "--". |
'--'
|
text_position |
float
|
Relative position (0-1) for the equation text. Defaults to 0.6. |
0.6
|
show_equation |
bool
|
Whether to display the equation on the plot. Defaults to True. |
True
|
show_r2 |
bool
|
Whether to display the R² value on the plot. Defaults to True. |
True
|
kwargs |
Any
|
Additional keyword arguments to pass to the plot function. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Axes |
Axes
|
The matplotlib axes with the regression line added. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the plot contains no visible lines, scatter points, or bar patches, or if regression_type is not supported. |
Examples:
Basic linear regression (backward compatible):
Power law regression for price elasticity:
Bar chart with regression line:
>>> ax = df.plot.bar(x='category', y='sales')
>>> gu.add_regression_line(ax, regression_type="linear")
Source code in openretailscience/plots/styles/graph_utils.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 | |
add_source_text(ax, source_text, font_size=None, vertical_padding=2, is_venn_diagram=False)
Add source text to the bottom left corner of a graph using styling helpers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax |
Axes
|
The graph to add the source text to. |
required |
source_text |
str
|
The source text. |
required |
font_size |
float
|
The font size of the source text. If None, uses styling context default. |
None
|
vertical_padding |
float
|
The padding in ems below the x-axis label. Defaults to 2. |
2
|
is_venn_diagram |
bool
|
Flag to indicate if the diagram is a Venn diagram.
If True, |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Text |
Text
|
The source text. |
Source code in openretailscience/plots/styles/graph_utils.py
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
get_decimals(ylim, tick_values, max_decimals=10)
Helper function for the human_format function that determines the number of decimals to use for the y-axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ylim |
tuple[float, float]
|
The y-axis limits. |
required |
tick_values |
list[float]
|
The y-axis tick values. |
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
human_format(num, pos=None, decimals=0, prefix='')
Format a number in a human-readable format for Matplotlib, discarding trailing zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num |
float
|
The number to format. |
required |
pos |
int
|
The position. Defaults to None. Only used for Matplotlib compatibility. |
None
|
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
not_none(value1, value2)
Helper function that returns the first value that is not None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value1 |
Any
|
The first value. |
required |
value2 |
Any
|
The second value. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The first value that is not None. |
Source code in openretailscience/plots/styles/graph_utils.py
set_axis_percent(fmt_axis, xmax=1, decimals=None, symbol='%')
Format an axis to display values as percentages.
This function configures a matplotlib axis to display its tick labels as percentages using matplotlib's PercentFormatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt_axis |
YAxis | XAxis
|
The axis to format (either ax.yaxis or ax.xaxis). |
required |
xmax |
float
|
The value that represents 100%. Defaults to 1. |
1
|
decimals |
int | None
|
Number of decimal places to include. If None, automatically selects based on data range. Defaults to None. |
None
|
symbol |
str | None
|
The symbol to use for percentage. If None, no symbol is displayed. Defaults to "%". |
'%'
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
The function modifies the axis formatter in place. |
Example
Source code in openretailscience/plots/styles/graph_utils.py
standard_graph_styles(ax, title=None, x_label=None, y_label=None, title_pad=None, x_label_pad=None, y_label_pad=None, legend_title=None, move_legend_outside=False, show_legend=True)
Apply standard styles to a Matplotlib graph using styling helpers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax |
Axes
|
The graph to apply the styles to. |
required |
title |
str
|
The title of the graph. Defaults to None. |
None
|
x_label |
str
|
The x-axis label. Defaults to None. |
None
|
y_label |
str
|
The y-axis label. Defaults to None. |
None
|
title_pad |
int
|
The padding above the title. Defaults to styling context default. |
None
|
x_label_pad |
int
|
The padding below the x-axis label. Defaults to styling context default. |
None
|
y_label_pad |
int
|
The padding to the left of the y-axis label. Defaults to styling context default. |
None
|
legend_title |
str
|
The title of the legend. If None, no legend title is applied. Defaults to None. |
None
|
move_legend_outside |
bool
|
Whether to move the legend outside the plot. Defaults to False. |
False
|
show_legend |
bool
|
Whether to display the legend or not. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Axes |
Axes
|
The graph with the styles applied. |
Source code in openretailscience/plots/styles/graph_utils.py
standard_tick_styles(ax)
Apply standard tick styles using styling helpers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax |
Axes
|
The graph to apply the styles to. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Axes |
Axes
|
The graph with the styles applied. |
Source code in openretailscience/plots/styles/graph_utils.py
truncate_to_x_digits(num_str, digits)
Truncate a human-formatted number to the first num_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'). |