direl-ts-tool-kit 0.9.1__py3-none-any.whl → 0.10.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -126,6 +126,137 @@ def plot_time_series(
126
126
  return fig
127
127
 
128
128
 
129
+ def plot_forecast(
130
+ df_hist,
131
+ df_ts,
132
+ variable,
133
+ units="",
134
+ color_hist="BLUE_LINES",
135
+ color_forecast="RED_LINES",
136
+ time_unit="Year",
137
+ rot=90,
138
+ auto_format_label=True,
139
+ ):
140
+ """
141
+ Plots historical data alongside a forecast with a shaded confidence interval.
142
+
143
+ This function combines a historical time series with a forecast period,
144
+ automatically formatting axes, labels, and applying professional styling.
145
+ It expects the forecast DataFrame to include lower and upper bound columns
146
+ named '{variable}_lower' and '{variable}_upper'.
147
+
148
+ Parameters
149
+ ----------
150
+ df_hist : pd.DataFrame
151
+ The DataFrame containing historical data. Must have a DatetimeIndex.
152
+ df_ts : pd.DataFrame
153
+ The DataFrame containing the forecast data and confidence intervals.
154
+ Must have a DatetimeIndex.
155
+ variable : str
156
+ The name of the column to plot in both DataFrames.
157
+ units : str, optional
158
+ Units to display next to the variable name on the y-axis (e.g., 'USD').
159
+ Defaults to "".
160
+ color : str, optional
161
+ Key corresponding to the line color for the forecast in the
162
+ global 'paper_colors' dictionary. Defaults to "BLUE_LINES".
163
+ time_unit : str, optional
164
+ The time granularity of the data to define x-axis tick locators.
165
+ Options: 'Year', 'Month', 'Weekday', 'Day' or 'Hour'. Defaults to "Year".
166
+ rot : int, optional
167
+ Rotation angle for the x-axis tick labels. Defaults to 90.
168
+ auto_format_label : bool, optional
169
+ If True, automatically formats the Y-axis label into Title Case.
170
+ Defaults to True.
171
+
172
+ Returns
173
+ -------
174
+ matplotlib.figure.Figure
175
+ The generated matplotlib figure object.
176
+
177
+ Notes
178
+ -----
179
+ The confidence interval is shaded using 'GRAY_BARS' from the paper_colors
180
+ palette with a transparency of 0.3.
181
+ """
182
+
183
+ fig, ax = plt.subplots()
184
+
185
+ ax.plot(
186
+ df_hist.index,
187
+ df_hist[variable],
188
+ linewidth=2,
189
+ color=paper_colors[color_hist],
190
+ label="Historical",
191
+ )
192
+
193
+ ax.plot(
194
+ df_ts.index,
195
+ df_ts[variable],
196
+ linewidth=3,
197
+ color=paper_colors[color_forecast],
198
+ label="Forecast",
199
+ )
200
+
201
+ ax.fill_between(
202
+ df_ts.index,
203
+ df_ts[variable + "_lower"],
204
+ df_ts[variable + "_upper"],
205
+ color=paper_colors["GRAY_BARS"],
206
+ alpha=0.3,
207
+ edgecolor="none",
208
+ label="Confidence Interval",
209
+ )
210
+
211
+ temp_variable = variable
212
+ if auto_format_label:
213
+ if "-" in temp_variable:
214
+ temp_variable = "-".join(
215
+ [
216
+ j.title() if i == 0 else j.lower()
217
+ for i, j in enumerate(temp_variable.split("-"))
218
+ ]
219
+ )
220
+ elif "_" in temp_variable:
221
+ temp_variable = " ".join(
222
+ [
223
+ j.title() if i == 0 else j.lower()
224
+ for i, j in enumerate(temp_variable.split("_"))
225
+ ]
226
+ )
227
+ else:
228
+ temp_variable = " ".join(
229
+ [
230
+ j.title() if i == 0 else j.lower()
231
+ for i, j in enumerate(temp_variable.split())
232
+ ]
233
+ )
234
+
235
+ ax.set(xlabel=f"{time_unit}", ylabel=f"{temp_variable} {units}")
236
+ ax.ticklabel_format(style="sci", axis="y", scilimits=(0, 0))
237
+
238
+ locators = {
239
+ "Year": (mdates.YearLocator(), mdates.MonthLocator()),
240
+ "Month": (mdates.MonthLocator(), mdates.WeekdayLocator()),
241
+ "Weekday": (mdates.WeekdayLocator(), mdates.DayLocator()),
242
+ "Day": (mdates.DayLocator(), mdates.HourLocator()),
243
+ "Hour": (mdates.HourLocator(), mdates.MinuteLocator()),
244
+ }
245
+
246
+ if time_unit in locators:
247
+ major, minor = locators[time_unit]
248
+ ax.xaxis.set_major_locator(major)
249
+ ax.xaxis.set_minor_locator(minor)
250
+
251
+ ax.tick_params(axis="x", rotation=rot)
252
+ ax.grid(which="both")
253
+ ax.grid(which="minor", alpha=0.6, linestyle=":")
254
+ ax.grid(which="major", alpha=0.8, linestyle="--")
255
+ ax.legend(loc="center left", bbox_to_anchor=(1, 0.5))
256
+
257
+ return fig
258
+
259
+
129
260
  def plot_interpolation_analysis(
130
261
  df_original,
131
262
  variable,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: direl-ts-tool-kit
3
- Version: 0.9.1
3
+ Version: 0.10.2
4
4
  Summary: A toolbox for time series analysis and visualization.
5
5
  Home-page: https://gitlab.com/direl/direl_tool_kit
6
6
  Author: Diego Restrepo-Leal
@@ -0,0 +1,11 @@
1
+ direl_ts_tool_kit/__init__.py,sha256=W99Wd3BeEFKOxT51TApURElbDJvqIjD8u_-qDoCYSJ0,94
2
+ direl_ts_tool_kit/plot/__init__.py,sha256=CMwyv-kiE74nwr3MJPL7gWIJmcfZ8UQCRu7mBGGQ4rI,49
3
+ direl_ts_tool_kit/plot/plot_style.py,sha256=5YxoLXlYvzleTnBEGPwCmHQIJ0S96KPJspq_n-qMvpw,1069
4
+ direl_ts_tool_kit/plot/plot_ts.py,sha256=AnC9qBHBiHhHMDhyawt7hASofLbuqLYgLOQ5O862APs,23608
5
+ direl_ts_tool_kit/utilities/__init__.py,sha256=jMtxYZUtwlhgI99sxe_8MMzsDnxtbTP7Ivh9tUOeIwQ,25
6
+ direl_ts_tool_kit/utilities/data_prep.py,sha256=k3eOwQEEd5mxy2DtT_Gdo7BhkzEmSQqvMJ89y8mH5CQ,6024
7
+ direl_ts_tool_kit-0.10.2.dist-info/licenses/LICENCE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ direl_ts_tool_kit-0.10.2.dist-info/METADATA,sha256=zXJ_tlgqUQJCsesc8uTZ6eqKXTR4rDBm1cL1RUbMjFg,5774
9
+ direl_ts_tool_kit-0.10.2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
10
+ direl_ts_tool_kit-0.10.2.dist-info/top_level.txt,sha256=vMCRudnGnsdRg_6fUftnG8PF2Y1m0bjBDMf3pCAp6bc,18
11
+ direl_ts_tool_kit-0.10.2.dist-info/RECORD,,
@@ -1,11 +0,0 @@
1
- direl_ts_tool_kit/__init__.py,sha256=W99Wd3BeEFKOxT51TApURElbDJvqIjD8u_-qDoCYSJ0,94
2
- direl_ts_tool_kit/plot/__init__.py,sha256=CMwyv-kiE74nwr3MJPL7gWIJmcfZ8UQCRu7mBGGQ4rI,49
3
- direl_ts_tool_kit/plot/plot_style.py,sha256=5YxoLXlYvzleTnBEGPwCmHQIJ0S96KPJspq_n-qMvpw,1069
4
- direl_ts_tool_kit/plot/plot_ts.py,sha256=XQfz9deexMLvzhM6ABD_eGghVpaAD3Vy0aRzSEny1Yc,19480
5
- direl_ts_tool_kit/utilities/__init__.py,sha256=jMtxYZUtwlhgI99sxe_8MMzsDnxtbTP7Ivh9tUOeIwQ,25
6
- direl_ts_tool_kit/utilities/data_prep.py,sha256=k3eOwQEEd5mxy2DtT_Gdo7BhkzEmSQqvMJ89y8mH5CQ,6024
7
- direl_ts_tool_kit-0.9.1.dist-info/licenses/LICENCE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- direl_ts_tool_kit-0.9.1.dist-info/METADATA,sha256=J8WGPY3VWni35UwIz0xm50qDhnFKLHA3mO_4vVb-Qek,5773
9
- direl_ts_tool_kit-0.9.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
10
- direl_ts_tool_kit-0.9.1.dist-info/top_level.txt,sha256=vMCRudnGnsdRg_6fUftnG8PF2Y1m0bjBDMf3pCAp6bc,18
11
- direl_ts_tool_kit-0.9.1.dist-info/RECORD,,