Period On Period Plot
Period on period module.
This module provides functionality for plotting multiple overlapping time periods from the same time series on a single line chart using matplotlib.
The plot
function is useful for visual comparisons of temporal trends
across different time windows, with each time window plotted as a separate line
but aligned to a common starting point.
Example use case: Comparing sales data across multiple promotional weeks or seasonal periods.
plot(df, x_col, value_col, periods, x_label=None, y_label=None, title=None, source_text=None, legend_title=None, move_legend_outside=False, ax=None, **kwargs)
Plot multiple overlapping periods from a single time series as individual lines.
This function is used to align and overlay several time intervals from the same dataset to facilitate visual comparison. Each period is realigned to the reference start date and plotted as a separate line using a distinct linestyle.
Note
The periods
argument accepts a list of (start_date, end_date) tuples,
which define the time windows to overlay. Each element in the tuple can be either
a string (e.g., "2022-01-01") or a datetime
object. You can use
find_overlapping_periods
from pyretailscience.utils.date
to generate
the periods
input automatically.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Input DataFrame containing the time series data. |
required |
x_col |
str
|
Name of the column representing datetime values. |
required |
value_col |
str
|
Name of the column representing the y-axis values (e.g. sales, counts). |
required |
periods |
List[Tuple[Union[str, datetime], Union[str, datetime]]]
|
A list of (start_date, end_date) tuples representing the periods to plot. |
required |
x_label |
Optional[str]
|
Custom label for the x-axis. |
None
|
y_label |
Optional[str]
|
Custom label for the y-axis. |
None
|
title |
Optional[str]
|
Title for the plot. |
None
|
source_text |
Optional[str]
|
Text to show below the plot as a data source. |
None
|
legend_title |
Optional[str]
|
Title for the plot legend. |
None
|
move_legend_outside |
bool
|
Whether to place the legend outside the plot area. |
False
|
ax |
Optional[Axes]
|
Matplotlib Axes object to draw on. If None, a new one is created. |
None
|
**kwargs |
dict[str, any]
|
Additional keyword arguments passed to the base line plot function. |
{}
|
Returns:
Type | Description |
---|---|
Axes
|
matplotlib.axes.Axes: The matplotlib Axes object with the completed plot. |
Raises:
Type | Description |
---|---|
ValueError
|
The 'periods' list must contain at least two (start, end) tuples for comparison. |
Source code in pyretailscience/plots/period_on_period.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 |
|