Broken Timeline Plot
This module provides functionality for creating broken timeline plots from pandas DataFrames.
A broken timeline plot visualizes data availability across categories over time, showing periods where data is available as horizontal bars, with gaps indicating missing data periods.
Features
- Multiple Categories: Support for displaying multiple categories with different colors
- Customizable Periods: Aggregate data by different time periods (daily, weekly)
- Threshold Filtering: Filter out values below a specified threshold
- Date Formatting: Uses matplotlib's ConciseDateFormatter for clean date axis labels
Use Cases
- Data Quality Assessment: Visualize data availability gaps across categories/segments over time
- Product Availability Analysis: Identify periods with stock outs by store/category
- Seasonality Analysis: Assess to look for period of low sales that may indicate seasonality or other trends
plot(df, category_col, value_col, title=None, x_label=None, y_label=None, ax=None, source_text=None, period='D', agg_func='sum', threshold_value=None, bar_height=0.8, figsize=None, **kwargs)
Creates a broken timeline plot showing data availability across categories over time.
Shows periods where data is available as horizontal bars, with gaps indicating missing data periods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The input DataFrame containing the data to be plotted. |
required |
category_col |
str
|
The column containing categories to display on y-axis. |
required |
value_col |
str
|
The column containing values to determine data availability. |
required |
title |
str
|
The title of the plot. Defaults to None. |
None
|
x_label |
str
|
The label for the x-axis. Defaults to None. |
None
|
y_label |
str
|
The label for the y-axis. Defaults to None. |
None
|
ax |
Axes
|
The Matplotlib Axes object to plot on. Defaults to None. |
None
|
source_text |
str
|
Text to be displayed as a source at the bottom of the plot. Defaults to None. |
None
|
period |
str
|
Period for aggregating data using pandas to_period ("D", "W"). Defaults to "D". |
'D'
|
agg_func |
str
|
The aggregation function to apply to the value_col when grouping by period. Defaults to "sum". |
'sum'
|
threshold_value |
float
|
Values below this threshold are considered gaps. Defaults to None. |
None
|
bar_height |
float
|
Height of timeline bars as fraction of available space. Defaults to 0.8. |
0.8
|
figsize |
tuple[int, int] | None
|
tuple[int, int] | None = None, |
None
|
**kwargs |
dict[str, Any]
|
Additional keyword arguments for matplotlib broken_barh function. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
SubplotBase |
SubplotBase
|
The Matplotlib Axes object with the generated plot. |
Raises:
Type | Description |
---|---|
ValueError
|
If DataFrame is empty, required columns are missing, or invalid period specified. |
KeyError
|
If specified columns don't exist in the DataFrame. |
Source code in pyretailscience/plots/broken_timeline.py
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|