Graph Utils
Helper functions for styling graphs.
GraphStyles
A class to hold the styles for a graph.
Source code in pyretailscience/style/graph_utils.py
add_regression_line(ax, color='red', linestyle='--', text_position=0.6, show_equation=True, show_r2=True, **kwargs)
Add a regression line to a plot.
This function examines the data in a matplotlib Axes object and adds a linear regression line to it. It can work with both line plots and scatter plots, and can handle both numeric and datetime x-axis values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ax |
Axes
|
The matplotlib axes object containing the plot (line or scatter). |
required |
color |
str
|
Color of the regression line. Defaults to "red". |
'red'
|
linestyle |
str
|
Style of the regression line. Defaults to "--". |
'--'
|
alpha |
float
|
Transparency of the regression line. Defaults to 0.8. |
required |
linewidth |
float
|
Width of the regression line. Defaults to 2.0. |
required |
label |
str
|
Label for the regression line in the legend. Defaults to "Regression Line". |
required |
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 |
dict[str, 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 or scatter points. |
Source code in pyretailscience/style/graph_utils.py
add_source_text(ax, source_text, font_size=GraphStyles.DEFAULT_AXIS_LABEL_FONT_SIZE, vertical_padding=2, is_venn_diagram=False)
Add source text to the bottom left corner of a graph.
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. Defaults to GraphStyles.DEFAULT_AXIS_LABEL_FONT_SIZE. |
DEFAULT_AXIS_LABEL_FONT_SIZE
|
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 pyretailscience/style/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 pyretailscience/style/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 100. |
10
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of decimals to use. |
Source code in pyretailscience/style/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 pyretailscience/style/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 pyretailscience/style/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 pyretailscience/style/graph_utils.py
standard_graph_styles(ax, title=None, x_label=None, y_label=None, title_pad=GraphStyles.DEFAULT_TITLE_PAD, x_label_pad=GraphStyles.DEFAULT_AXIS_LABEL_PAD, y_label_pad=GraphStyles.DEFAULT_AXIS_LABEL_PAD, legend_title=None, move_legend_outside=False, show_legend=True)
Apply standard styles to a Matplotlib graph.
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 GraphStyles.DEFAULT_TITLE_PAD. |
DEFAULT_TITLE_PAD
|
x_label_pad |
int
|
The padding below the x-axis label. Defaults to GraphStyles.DEFAULT_AXIS_LABEL_PAD. |
DEFAULT_AXIS_LABEL_PAD
|
y_label_pad |
int
|
The padding to the left of the y-axis label. Defaults to GraphStyles.DEFAULT_AXIS_LABEL_PAD. |
DEFAULT_AXIS_LABEL_PAD
|
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 pyretailscience/style/graph_utils.py
standard_tick_styles(ax)
Apply standard tick styles to a Matplotlib graph.
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 pyretailscience/style/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'). |