plot.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the BSD-style license found in the
  5. # LICENSE file in the root directory of this source tree. An additional grant
  6. # of patent rights can be found in the PATENTS file in the same directory.
  7. from __future__ import absolute_import
  8. from __future__ import division
  9. from __future__ import print_function
  10. from __future__ import unicode_literals
  11. import logging
  12. import numpy as np
  13. import pandas as pd
  14. from fbprophet.diagnostics import performance_metrics
  15. logging.basicConfig()
  16. logger = logging.getLogger(__name__)
  17. try:
  18. from matplotlib import pyplot as plt
  19. from matplotlib.dates import MonthLocator, num2date
  20. from matplotlib.ticker import FuncFormatter
  21. except ImportError:
  22. logger.error('Importing matplotlib failed. Plotting will not work.')
  23. def plot(
  24. m, fcst, ax=None, uncertainty=True, plot_cap=True, xlabel='ds', ylabel='y',
  25. ):
  26. """Plot the Prophet forecast.
  27. Parameters
  28. ----------
  29. m: Prophet model.
  30. fcst: pd.DataFrame output of m.predict.
  31. ax: Optional matplotlib axes on which to plot.
  32. uncertainty: Optional boolean to plot uncertainty intervals.
  33. plot_cap: Optional boolean indicating if the capacity should be shown
  34. in the figure, if available.
  35. xlabel: Optional label name on X-axis
  36. ylabel: Optional label name on Y-axis
  37. Returns
  38. -------
  39. A matplotlib figure.
  40. """
  41. if ax is None:
  42. fig = plt.figure(facecolor='w', figsize=(10, 6))
  43. ax = fig.add_subplot(111)
  44. else:
  45. fig = ax.get_figure()
  46. fcst_t = fcst['ds'].dt.to_pydatetime()
  47. ax.plot(m.history['ds'].dt.to_pydatetime(), m.history['y'], 'k.')
  48. ax.plot(fcst_t, fcst['yhat'], ls='-', c='#0072B2')
  49. if 'cap' in fcst and plot_cap:
  50. ax.plot(fcst_t, fcst['cap'], ls='--', c='k')
  51. if m.logistic_floor and 'floor' in fcst and plot_cap:
  52. ax.plot(fcst_t, fcst['floor'], ls='--', c='k')
  53. if uncertainty:
  54. ax.fill_between(fcst_t, fcst['yhat_lower'], fcst['yhat_upper'],
  55. color='#0072B2', alpha=0.2)
  56. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  57. ax.set_xlabel(xlabel)
  58. ax.set_ylabel(ylabel)
  59. fig.tight_layout()
  60. return fig
  61. def plot_components(
  62. m, fcst, uncertainty=True, plot_cap=True, weekly_start=0, yearly_start=0,
  63. ):
  64. """Plot the Prophet forecast components.
  65. Will plot whichever are available of: trend, holidays, weekly
  66. seasonality, and yearly seasonality.
  67. Parameters
  68. ----------
  69. m: Prophet model.
  70. fcst: pd.DataFrame output of m.predict.
  71. uncertainty: Optional boolean to plot uncertainty intervals.
  72. plot_cap: Optional boolean indicating if the capacity should be shown
  73. in the figure, if available.
  74. weekly_start: Optional int specifying the start day of the weekly
  75. seasonality plot. 0 (default) starts the week on Sunday. 1 shifts
  76. by 1 day to Monday, and so on.
  77. yearly_start: Optional int specifying the start day of the yearly
  78. seasonality plot. 0 (default) starts the year on Jan 1. 1 shifts
  79. by 1 day to Jan 2, and so on.
  80. Returns
  81. -------
  82. A matplotlib figure.
  83. """
  84. # Identify components to be plotted
  85. components = ['trend']
  86. if m.holidays is not None and 'holidays' in fcst:
  87. components.append('holidays')
  88. components.extend([name for name in m.seasonalities
  89. if name in fcst])
  90. if len(m.extra_regressors) > 0 and 'extra_regressors' in fcst:
  91. components.append('extra_regressors')
  92. npanel = len(components)
  93. fig, axes = plt.subplots(npanel, 1, facecolor='w',
  94. figsize=(9, 3 * npanel))
  95. if npanel == 1:
  96. axes = [axes]
  97. for ax, plot_name in zip(axes, components):
  98. if plot_name == 'trend':
  99. plot_forecast_component(
  100. m=m, fcst=fcst, name='trend', ax=ax, uncertainty=uncertainty,
  101. plot_cap=plot_cap,
  102. )
  103. elif plot_name == 'holidays':
  104. plot_forecast_component(
  105. m=m, fcst=fcst, name='holidays', ax=ax,
  106. uncertainty=uncertainty, plot_cap=False,
  107. )
  108. elif plot_name == 'weekly':
  109. plot_weekly(
  110. m=m, ax=ax, uncertainty=uncertainty, weekly_start=weekly_start,
  111. )
  112. elif plot_name == 'yearly':
  113. plot_yearly(
  114. m=m, ax=ax, uncertainty=uncertainty, yearly_start=yearly_start,
  115. )
  116. elif plot_name == 'extra_regressors':
  117. plot_forecast_component(
  118. m=m, fcst=fcst, name='extra_regressors', ax=ax,
  119. uncertainty=uncertainty, plot_cap=False,
  120. )
  121. else:
  122. plot_seasonality(
  123. m=m, name=plot_name, ax=ax, uncertainty=uncertainty,
  124. )
  125. fig.tight_layout()
  126. return fig
  127. def plot_forecast_component(
  128. m, fcst, name, ax=None, uncertainty=True, plot_cap=False,
  129. ):
  130. """Plot a particular component of the forecast.
  131. Parameters
  132. ----------
  133. m: Prophet model.
  134. fcst: pd.DataFrame output of m.predict.
  135. name: Name of the component to plot.
  136. ax: Optional matplotlib Axes to plot on.
  137. uncertainty: Optional boolean to plot uncertainty intervals.
  138. plot_cap: Optional boolean indicating if the capacity should be shown
  139. in the figure, if available.
  140. Returns
  141. -------
  142. a list of matplotlib artists
  143. """
  144. artists = []
  145. if not ax:
  146. fig = plt.figure(facecolor='w', figsize=(10, 6))
  147. ax = fig.add_subplot(111)
  148. fcst_t = fcst['ds'].dt.to_pydatetime()
  149. artists += ax.plot(fcst_t, fcst[name], ls='-', c='#0072B2')
  150. if 'cap' in fcst and plot_cap:
  151. artists += ax.plot(fcst_t, fcst['cap'], ls='--', c='k')
  152. if m.logistic_floor and 'floor' in fcst and plot_cap:
  153. ax.plot(fcst_t, fcst['floor'], ls='--', c='k')
  154. if uncertainty:
  155. artists += [ax.fill_between(
  156. fcst_t, fcst[name + '_lower'], fcst[name + '_upper'],
  157. color='#0072B2', alpha=0.2)]
  158. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  159. ax.set_xlabel('ds')
  160. ax.set_ylabel(name)
  161. return artists
  162. def seasonality_plot_df(m, ds):
  163. """Prepare dataframe for plotting seasonal components.
  164. Parameters
  165. ----------
  166. m: Prophet model.
  167. ds: List of dates for column ds.
  168. Returns
  169. -------
  170. A dataframe with seasonal components on ds.
  171. """
  172. df_dict = {'ds': ds, 'cap': 1., 'floor': 0.}
  173. for name in m.extra_regressors:
  174. df_dict[name] = 0.
  175. df = pd.DataFrame(df_dict)
  176. df = m.setup_dataframe(df)
  177. return df
  178. def plot_weekly(m, ax=None, uncertainty=True, weekly_start=0):
  179. """Plot the weekly component of the forecast.
  180. Parameters
  181. ----------
  182. m: Prophet model.
  183. ax: Optional matplotlib Axes to plot on. One will be created if this
  184. is not provided.
  185. uncertainty: Optional boolean to plot uncertainty intervals.
  186. weekly_start: Optional int specifying the start day of the weekly
  187. seasonality plot. 0 (default) starts the week on Sunday. 1 shifts
  188. by 1 day to Monday, and so on.
  189. Returns
  190. -------
  191. a list of matplotlib artists
  192. """
  193. artists = []
  194. if not ax:
  195. fig = plt.figure(facecolor='w', figsize=(10, 6))
  196. ax = fig.add_subplot(111)
  197. # Compute weekly seasonality for a Sun-Sat sequence of dates.
  198. days = (pd.date_range(start='2017-01-01', periods=7) +
  199. pd.Timedelta(days=weekly_start))
  200. df_w = seasonality_plot_df(m, days)
  201. seas = m.predict_seasonal_components(df_w)
  202. days = days.weekday_name
  203. artists += ax.plot(range(len(days)), seas['weekly'], ls='-',
  204. c='#0072B2')
  205. if uncertainty:
  206. artists += [ax.fill_between(range(len(days)),
  207. seas['weekly_lower'], seas['weekly_upper'],
  208. color='#0072B2', alpha=0.2)]
  209. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  210. ax.set_xticks(range(len(days)))
  211. ax.set_xticklabels(days)
  212. ax.set_xlabel('Day of week')
  213. ax.set_ylabel('weekly')
  214. return artists
  215. def plot_yearly(m, ax=None, uncertainty=True, yearly_start=0):
  216. """Plot the yearly component of the forecast.
  217. Parameters
  218. ----------
  219. m: Prophet model.
  220. ax: Optional matplotlib Axes to plot on. One will be created if
  221. this is not provided.
  222. uncertainty: Optional boolean to plot uncertainty intervals.
  223. yearly_start: Optional int specifying the start day of the yearly
  224. seasonality plot. 0 (default) starts the year on Jan 1. 1 shifts
  225. by 1 day to Jan 2, and so on.
  226. Returns
  227. -------
  228. a list of matplotlib artists
  229. """
  230. artists = []
  231. if not ax:
  232. fig = plt.figure(facecolor='w', figsize=(10, 6))
  233. ax = fig.add_subplot(111)
  234. # Compute yearly seasonality for a Jan 1 - Dec 31 sequence of dates.
  235. days = (pd.date_range(start='2017-01-01', periods=365) +
  236. pd.Timedelta(days=yearly_start))
  237. df_y = seasonality_plot_df(m, days)
  238. seas = m.predict_seasonal_components(df_y)
  239. artists += ax.plot(
  240. df_y['ds'].dt.to_pydatetime(), seas['yearly'], ls='-', c='#0072B2')
  241. if uncertainty:
  242. artists += [ax.fill_between(
  243. df_y['ds'].dt.to_pydatetime(), seas['yearly_lower'],
  244. seas['yearly_upper'], color='#0072B2', alpha=0.2)]
  245. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  246. months = MonthLocator(range(1, 13), bymonthday=1, interval=2)
  247. ax.xaxis.set_major_formatter(FuncFormatter(
  248. lambda x, pos=None: '{dt:%B} {dt.day}'.format(dt=num2date(x))))
  249. ax.xaxis.set_major_locator(months)
  250. ax.set_xlabel('Day of year')
  251. ax.set_ylabel('yearly')
  252. return artists
  253. def plot_seasonality(m, name, ax=None, uncertainty=True):
  254. """Plot a custom seasonal component.
  255. Parameters
  256. ----------
  257. m: Prophet model.
  258. name: Seasonality name, like 'daily', 'weekly'.
  259. ax: Optional matplotlib Axes to plot on. One will be created if
  260. this is not provided.
  261. uncertainty: Optional boolean to plot uncertainty intervals.
  262. Returns
  263. -------
  264. a list of matplotlib artists
  265. """
  266. artists = []
  267. if not ax:
  268. fig = plt.figure(facecolor='w', figsize=(10, 6))
  269. ax = fig.add_subplot(111)
  270. # Compute seasonality from Jan 1 through a single period.
  271. start = pd.to_datetime('2017-01-01 0000')
  272. period = m.seasonalities[name]['period']
  273. end = start + pd.Timedelta(days=period)
  274. plot_points = 200
  275. days = pd.to_datetime(np.linspace(start.value, end.value, plot_points))
  276. df_y = seasonality_plot_df(m, days)
  277. seas = m.predict_seasonal_components(df_y)
  278. artists += ax.plot(df_y['ds'].dt.to_pydatetime(), seas[name], ls='-',
  279. c='#0072B2')
  280. if uncertainty:
  281. artists += [ax.fill_between(
  282. df_y['ds'].dt.to_pydatetime(), seas[name + '_lower'],
  283. seas[name + '_upper'], color='#0072B2', alpha=0.2)]
  284. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  285. xticks = pd.to_datetime(np.linspace(start.value, end.value, 7)
  286. ).to_pydatetime()
  287. ax.set_xticks(xticks)
  288. if period <= 2:
  289. fmt_str = '{dt:%T}'
  290. elif period < 14:
  291. fmt_str = '{dt:%m}/{dt:%d} {dt:%R}'
  292. else:
  293. fmt_str = '{dt:%m}/{dt:%d}'
  294. ax.xaxis.set_major_formatter(FuncFormatter(
  295. lambda x, pos=None: fmt_str.format(dt=num2date(x))))
  296. ax.set_xlabel('ds')
  297. ax.set_ylabel(name)
  298. return artists
  299. def add_changepoints_to_plot(
  300. ax, m, fcst, threshold=0.01, cp_color='r', cp_linestyle='--', trend=True,
  301. ):
  302. """Add markers for significant changepoints to prophet forecast plot.
  303. Example:
  304. fig = m.plot(forecast)
  305. add_changepoints_to_plot(fig.gca(), m, forecast)
  306. Parameters
  307. ----------
  308. ax: axis on which to overlay changepoint markers.
  309. m: Prophet model.
  310. fcst: Forecast output from m.predict.
  311. threshold: Threshold on trend change magnitude for significance.
  312. cp_color: Color of changepoint markers.
  313. cp_linestyle: Linestyle for changepoint markers.
  314. trend: If True, will also overlay the trend.
  315. Returns
  316. -------
  317. a list of matplotlib artists
  318. """
  319. artists = []
  320. if trend:
  321. artists.append(ax.plot(fcst['ds'], fcst['trend'], c=cp_color))
  322. signif_changepoints = m.changepoints[
  323. np.abs(np.nanmean(m.params['delta'], axis=0)) >= threshold
  324. ]
  325. for cp in signif_changepoints:
  326. artists.append(ax.axvline(x=cp, c=cp_color, ls=cp_linestyle))
  327. return artists
  328. def plot_cross_validation_metric(df_cv, metric, rolling_window=0.1, ax=None):
  329. """Plot a performance metric vs. forecast horizon from cross validation.
  330. Cross validation produces a collection of out-of-sample model predictions
  331. that can be compared to actual values, at a range of different horizons
  332. (distance from the cutoff). This computes a specified performance metric
  333. for each prediction, and aggregated over a rolling window with horizon.
  334. This uses fbprophet.diagnostics.performance_metrics to compute the metrics.
  335. Valid values of metric are 'mse', 'rmse', 'mae', 'mape', and 'coverage'.
  336. rolling_window is the proportion of data included in the rolling window of
  337. aggregation. The default value of 0.1 means 10% of data are included in the
  338. aggregation for computing the metric.
  339. As a concrete example, if metric='mse', then this plot will show the
  340. squared error for each cross validation prediction, along with the MSE
  341. averaged over rolling windows of 10% of the data.
  342. Parameters
  343. ----------
  344. df_cv: The output from fbprophet.diagnostics.cross_validation.
  345. metric: Metric name, one of ['mse', 'rmse', 'mae', 'mape', 'coverage'].
  346. rolling_window: Proportion of data to use for rolling average of metric.
  347. In [0, 1]. Defaults to 0.1.
  348. ax: Optional matplotlib axis on which to plot. If not given, a new figure
  349. will be created.
  350. Returns
  351. -------
  352. a matplotlib figure.
  353. """
  354. if ax is None:
  355. fig = plt.figure(facecolor='w', figsize=(10, 6))
  356. ax = fig.add_subplot(111)
  357. else:
  358. fig = ax.get_figure()
  359. # Get the metric at the level of individual predictions, and with the rolling window.
  360. df_none = performance_metrics(df_cv, metrics=[metric], rolling_window=0)
  361. df_h = performance_metrics(df_cv, metrics=[metric], rolling_window=rolling_window)
  362. # Some work because matplotlib does not handle timedelta
  363. # Target ~10 ticks.
  364. tick_w = max(df_none['horizon'].astype('timedelta64[ns]')) / 10.
  365. # Find the largest time resolution that has <1 unit per bin.
  366. dts = ['D', 'h', 'm', 's', 'ms', 'us', 'ns']
  367. dt_names = [
  368. 'days', 'hours', 'minutes', 'seconds', 'milliseconds', 'microseconds',
  369. 'nanoseconds'
  370. ]
  371. dt_conversions = [
  372. 24 * 60 * 60 * 10 ** 9,
  373. 60 * 60 * 10 ** 9,
  374. 60 * 10 ** 9,
  375. 10 ** 9,
  376. 10 ** 6,
  377. 10 ** 3,
  378. 1.,
  379. ]
  380. for i, dt in enumerate(dts):
  381. if np.timedelta64(1, dt) < np.timedelta64(tick_w, 'ns'):
  382. break
  383. x_plt = df_none['horizon'].astype('timedelta64[ns]').astype(int) / float(dt_conversions[i])
  384. x_plt_h = df_h['horizon'].astype('timedelta64[ns]').astype(int) / float(dt_conversions[i])
  385. ax.plot(x_plt, df_none[metric], '.', alpha=0.5, c='gray')
  386. ax.plot(x_plt_h, df_h[metric], '-', c='b')
  387. ax.grid(True)
  388. ax.set_xlabel('Horizon ({})'.format(dt_names[i]))
  389. ax.set_ylabel(metric)
  390. return fig