Skip to content

Instantly share code, notes, and snippets.

@chezou
Last active December 9, 2019 08:44
Show Gist options
  • Save chezou/71527cff1dc828f74d7f96a346b663a0 to your computer and use it in GitHub Desktop.
Save chezou/71527cff1dc828f74d7f96a346b663a0 to your computer and use it in GitHub Desktop.
Plot Facebook Prophet without model
import matplotlib as mlp
import pandas as pd
mlp.use("agg")
from matplotlib import pyplot as plt
from matplotlib.dates import (
MonthLocator,
num2date,
AutoDateLocator,
AutoDateFormatter,
)
# Equivalent code with model.plot()
# https://github.com/facebook/prophet/blob/ca9a49d328ab1f2a991f246a3ebc37a7f9c896c5/python/fbprophet/plot.py#L41-L88
# Assuming `df` is predicion results and `df_prev` is training data.
def plot(df, df_prev, xlabel='ds', ylabel='y'):
fig = plt.figure(facecolor="w", figsize=(10,6))
ax = fig.add_subplot(111)
fcst_t = pd.to_datetime(df["ds"])
ax.plot(pd.to_datetime(df_prev["ds"]), df_prev["y"], "k.")
ax.plot(fcst_t, df["yhat"], ls="-", c="#0072B2")
if "cap" in df:
ax.plot(fcst_t, df["cap"], ls="--", c="k")
if "floor" in df:
ax.plot(fsct_t, df["floor"], ls="--", c="k")
ax.fill_between(fcst_t, df["yhat_lower"], df["yhat_upper"], color="#0072B2", alpha=0.2)
locator = AutoDateLocator(interval_multiples=False)
formatter = AutoDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
fig.tight_layout()
return fig
plot(df_pred, df_orig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment