Period On Period Plot
Overlay several time windows of one series on a single line chart.
Each window is shifted by whole years onto the first period's year. Use
utils.date.find_overlapping_periods to build the periods. Use plots.time for one
continuous resampled series; use this module to compare multiple windows.
plot(df, x_col, value_col, periods, x_label=None, y_label=None, title=None, eyebrow=None, subtitle=None, source_text=None, legend_title=None, move_legend_outside=False, legend_style=None, ax=None, figsize=None, **kwargs)
Plot multiple overlapping periods from a single time series as individual lines.
Each period is shifted by start.year - first_period.start.year whole years, so the x-axis
is calendar position within the year of the first tuple; windows whose in-year start dates
differ do not line up at x=0. Periods with no matching rows are silently skipped.
Newer periods are drawn darker, thicker, and on top; a user-supplied linewidth overrides
the thickness gradient.
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[str | datetime, str | datetime]]
|
A list of at least two (start_date, end_date) tuples, each element a string or
datetime. Use |
required |
x_label
|
str
|
X-axis label; omitted means no label. |
None
|
y_label
|
str
|
Y-axis label; omitted means no label. |
None
|
title
|
str
|
Title for the plot. |
None
|
eyebrow
|
str
|
Small uppercase label rendered above the title. |
None
|
subtitle
|
str
|
Supporting copy rendered below the title. |
None
|
source_text
|
str
|
Text to show below the plot as a data source. |
None
|
legend_title
|
str
|
Title for the plot legend. |
None
|
move_legend_outside
|
bool
|
Whether to place the legend outside the plot area. |
False
|
legend_style
|
Literal['box', 'end_of_line']
|
How periods are labelled. |
None
|
ax
|
Axes
|
Matplotlib Axes object to draw on. If None, a new one is created. |
None
|
figsize
|
tuple[int, int]
|
Size of the new figure when |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Axes
|
matplotlib.axes.Axes: The matplotlib Axes object with the completed plot. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If periods contains fewer than two (start, end) tuples. |
ValueError
|
If legend_style is not one of |
Source code in openretailscience/plots/period_on_period.py
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |