Automatic forecasting procedure for time series data with strong seasonal effects and multiple seasons of historical data
—
Comprehensive plotting functionality for Prophet forecasts supporting both matplotlib and Plotly backends. Includes forecast visualization, component decomposition, seasonality analysis, and cross-validation metric plotting.
Static plotting functions using matplotlib backend.
def plot(
m, fcst, ax=None, uncertainty=True, plot_cap=True, xlabel='ds', ylabel='y',
figsize=(10, 6), include_legend=False
):
"""
Plot the Prophet forecast using matplotlib.
Parameters:
- m: Prophet, fitted Prophet object
- fcst: DataFrame, forecast output from Prophet.predict()
- ax: matplotlib axes, axes to plot on (optional)
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- plot_cap: bool, whether to plot carrying capacity for logistic growth (default: True)
- xlabel: str, label for X-axis (default: 'ds')
- ylabel: str, label for Y-axis (default: 'y')
- figsize: tuple, figure size in inches (default: (10, 6))
- include_legend: bool, whether to add legend to the plot (default: False)
Returns:
- matplotlib Figure object
"""
def plot_components(
m, fcst, uncertainty=True, plot_cap=True, weekly_start=0, yearly_start=0,
figsize=None
):
"""
Plot the Prophet forecast components using matplotlib.
Parameters:
- m: Prophet, fitted Prophet object
- fcst: DataFrame, forecast output from Prophet.predict()
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- plot_cap: bool, whether to plot carrying capacity (default: True)
- weekly_start: int, start day of weekly seasonality plot (0=Sunday, default: 0)
- yearly_start: int, start day of yearly seasonality plot (0=Jan 1, default: 0)
- figsize: tuple, figure size in inches (optional)
Returns:
- matplotlib Figure object with subplots for each component
"""
def plot_forecast_component(
m, fcst, name, ax=None, uncertainty=True, plot_cap=False, figsize=(10, 6)
):
"""
Plot an individual forecast component.
Parameters:
- m: Prophet, fitted Prophet object
- fcst: DataFrame, forecast output from Prophet.predict()
- name: str, name of component to plot
- ax: matplotlib axes, axes to plot on (optional)
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- plot_cap: bool, whether to plot carrying capacity (default: False)
- figsize: tuple, figure size in inches (default: (10, 6))
Returns:
- list of matplotlib artists
"""Interactive plotting functions using Plotly backend.
def plot_plotly(m, fcst, uncertainty=True, plot_cap=True, trend=False, changepoints=False,
changepoints_threshold=0.01, xlabel='ds', ylabel='y', figsize=(900, 600)):
"""
Plot the Prophet forecast using Plotly.
Parameters:
- m: Prophet, fitted Prophet object
- fcst: DataFrame, forecast output from Prophet.predict()
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- plot_cap: bool, whether to plot carrying capacity (default: True)
- trend: bool, whether to plot trend line (default: False)
- changepoints: bool, whether to plot changepoints (default: False)
- changepoints_threshold: float, threshold for changepoint significance (default: 0.01)
- xlabel: str, label for X-axis (default: 'ds')
- ylabel: str, label for Y-axis (default: 'y')
- figsize: tuple, plot size in pixels (default: (900, 600))
Returns:
- plotly Figure object
"""
def plot_components_plotly(
m, fcst, uncertainty=True, plot_cap=True, figsize=(900, 200)):
"""
Plot the Prophet forecast components using Plotly.
Parameters:
- m: Prophet, fitted Prophet object
- fcst: DataFrame, forecast output from Prophet.predict()
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- plot_cap: bool, whether to plot carrying capacity (default: True)
- figsize: tuple, subplot size in pixels (default: (900, 200))
Returns:
- plotly Figure object with subplots for each component
"""
def plot_forecast_component_plotly(m, fcst, name, uncertainty=True, plot_cap=False, figsize=(900, 300)):
"""
Plot an individual forecast component using Plotly.
Parameters:
- m: Prophet, fitted Prophet object
- fcst: DataFrame, forecast output from Prophet.predict()
- name: str, name of component to plot
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- plot_cap: bool, whether to plot carrying capacity (default: False)
- figsize: tuple, plot size in pixels (default: (900, 300))
Returns:
- plotly Figure object
"""Specialized plotting functions for seasonality analysis.
def plot_weekly(m, ax=None, uncertainty=True, weekly_start=0, figsize=(10, 6), name='weekly'):
"""
Plot the weekly seasonality component.
Parameters:
- m: Prophet, fitted Prophet object
- ax: matplotlib axes, axes to plot on (optional)
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- weekly_start: int, start day of weekly plot (0=Sunday, default: 0)
- figsize: tuple, figure size in inches (default: (10, 6))
- name: str, name of seasonality component (default: 'weekly')
Returns:
- list of matplotlib artists
"""
def plot_yearly(m, ax=None, uncertainty=True, yearly_start=0, figsize=(10, 6), name='yearly'):
"""
Plot the yearly seasonality component.
Parameters:
- m: Prophet, fitted Prophet object
- ax: matplotlib axes, axes to plot on (optional)
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- yearly_start: int, start day of yearly plot (0=Jan 1, default: 0)
- figsize: tuple, figure size in inches (default: (10, 6))
- name: str, name of seasonality component (default: 'yearly')
Returns:
- list of matplotlib artists
"""
def plot_seasonality(m, name, ax=None, uncertainty=True, figsize=(10, 6)):
"""
Plot a custom seasonality component.
Parameters:
- m: Prophet, fitted Prophet object
- name: str, name of seasonality component to plot
- ax: matplotlib axes, axes to plot on (optional)
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- figsize: tuple, figure size in inches (default: (10, 6))
Returns:
- list of matplotlib artists
"""
def plot_seasonality_plotly(m, name, uncertainty=True, figsize=(900, 300)):
"""
Plot a custom seasonality component using Plotly.
Parameters:
- m: Prophet, fitted Prophet object
- name: str, name of seasonality component to plot
- uncertainty: bool, whether to plot uncertainty intervals (default: True)
- figsize: tuple, plot size in pixels (default: (900, 300))
Returns:
- plotly Figure object
"""Additional plotting functionality for changepoints and cross-validation metrics.
def add_changepoints_to_plot(ax, m, fcst, threshold=0.01, **kwargs):
"""
Add changepoint indicators to an existing plot.
Parameters:
- ax: matplotlib axes, axes to add changepoints to
- m: Prophet, fitted Prophet object
- fcst: DataFrame, forecast output from Prophet.predict()
- threshold: float, minimum changepoint significance to plot (default: 0.01)
- **kwargs: additional arguments for changepoint styling
Returns:
- matplotlib axes with added changepoint indicators
"""
def plot_cross_validation_metric(
df_cv, metric, rolling_window=0.1, ax=None, figsize=(10, 6), color='b',
point_color='gray'
):
"""
Plot a cross-validation performance metric vs forecast horizon.
Parameters:
- df_cv: DataFrame, cross-validation results from cross_validation()
- metric: str, metric name to plot ('mse', 'rmse', 'mae', 'mape', 'mdape', 'smape', 'coverage')
- rolling_window: float, rolling window proportion for smoothing (default: 0.1)
- ax: matplotlib axes, axes to plot on (optional)
- figsize: tuple, figure size in inches (default: (10, 6))
- color: str, color for the rolling average line (default: 'b')
- point_color: str, color for individual data points (default: 'gray')
Returns:
- matplotlib axes
"""Helper functions for plot data preparation and formatting.
def seasonality_plot_df(m, ds):
"""
Prepare DataFrame for seasonality plotting.
Parameters:
- m: Prophet, fitted Prophet object
- ds: datetime or Series, dates for seasonality evaluation
Returns:
- DataFrame with seasonality values
"""
def set_y_as_percent(ax):
"""
Format y-axis as percentages.
Parameters:
- ax: matplotlib axes, axes to format
Returns:
- None (modifies axes in-place)
"""
def get_forecast_component_plotly_props(m, fcst, name):
"""
Get Plotly properties for forecast components.
Parameters:
- m: Prophet, fitted Prophet object
- fcst: DataFrame, forecast output
- name: str, component name
Returns:
- dict, Plotly properties for the component
"""
def get_seasonality_plotly_props(m, name):
"""
Get Plotly properties for seasonality components.
Parameters:
- m: Prophet, fitted Prophet object
- name: str, seasonality name
Returns:
- dict, Plotly properties for the seasonality
"""from prophet import Prophet
from prophet.plot import plot, plot_components
# Fit model and generate forecast
model = Prophet()
model.fit(df)
forecast = model.predict(future)
# Plot main forecast
fig1 = plot(model, forecast)
fig1.show()
# Plot components breakdown
fig2 = plot_components(model, forecast)
fig2.show()from prophet.plot import plot_plotly, plot_components_plotly
# Create interactive plots
fig_interactive = plot_plotly(model, forecast)
fig_interactive.show()
fig_components = plot_components_plotly(model, forecast)
fig_components.show()from prophet.plot import plot_yearly, plot_weekly, plot_seasonality
# Plot built-in seasonalities
fig_yearly = plot_yearly(model)
fig_weekly = plot_weekly(model)
# Plot custom seasonality (if added)
fig_custom = plot_seasonality(model, 'monthly')import matplotlib.pyplot as plt
from prophet.plot import add_changepoints_to_plot
# Create base forecast plot
fig, ax = plt.subplots(figsize=(12, 8))
plot(model, forecast, ax=ax)
# Add changepoint indicators
add_changepoints_to_plot(ax, model, forecast, threshold=0.01)
plt.show()Install with Tessl CLI
npx tessl i tessl/pypi-prophet