Plot the Prophet forecast. Parameters ---------- m: Prophet model. fcst: pd.DataFrame output of m.predict. ax: Optional matplotlib axes on which to plot. uncertainty: Optional boolean to plot uncertainty intervals, which will only be done if m.uncertainty_samples > 0
(
m, fcst, ax=None, uncertainty=True, plot_cap=True, xlabel='ds', ylabel='y',
figsize=(10, 6), include_legend=False
)
| 41 | |
| 42 | |
| 43 | def plot( |
| 44 | m, fcst, ax=None, uncertainty=True, plot_cap=True, xlabel='ds', ylabel='y', |
| 45 | figsize=(10, 6), include_legend=False |
| 46 | ): |
| 47 | """Plot the Prophet forecast. |
| 48 | |
| 49 | Parameters |
| 50 | ---------- |
| 51 | m: Prophet model. |
| 52 | fcst: pd.DataFrame output of m.predict. |
| 53 | ax: Optional matplotlib axes on which to plot. |
| 54 | uncertainty: Optional boolean to plot uncertainty intervals, which will |
| 55 | only be done if m.uncertainty_samples > 0. |
| 56 | plot_cap: Optional boolean indicating if the capacity should be shown |
| 57 | in the figure, if available. |
| 58 | xlabel: Optional label name on X-axis |
| 59 | ylabel: Optional label name on Y-axis |
| 60 | figsize: Optional tuple width, height in inches. |
| 61 | include_legend: Optional boolean to add legend to the plot. |
| 62 | |
| 63 | Returns |
| 64 | ------- |
| 65 | A matplotlib figure. |
| 66 | """ |
| 67 | user_provided_ax = False if ax is None else True |
| 68 | if ax is None: |
| 69 | fig = plt.figure(facecolor='w', figsize=figsize) |
| 70 | ax = fig.add_subplot(111) |
| 71 | else: |
| 72 | fig = ax.get_figure() |
| 73 | fcst_t = fcst['ds'] |
| 74 | ax.plot(m.history['ds'], m.history['y'], 'k.', |
| 75 | label='Observed data points') |
| 76 | ax.plot(fcst_t, fcst['yhat'], ls='-', c='#0072B2', label='Forecast') |
| 77 | if 'cap' in fcst and plot_cap: |
| 78 | ax.plot(fcst_t, fcst['cap'], ls='--', c='k', label='Maximum capacity') |
| 79 | if m.logistic_floor and 'floor' in fcst and plot_cap: |
| 80 | ax.plot(fcst_t, fcst['floor'], ls='--', c='k', label='Minimum capacity') |
| 81 | if uncertainty and m.uncertainty_samples: |
| 82 | ax.fill_between(fcst_t, fcst['yhat_lower'], fcst['yhat_upper'], |
| 83 | color='#0072B2', alpha=0.2, label='Uncertainty interval') |
| 84 | # Specify formatting to workaround matplotlib issue #12925 |
| 85 | locator = AutoDateLocator(interval_multiples=False) |
| 86 | formatter = AutoDateFormatter(locator) |
| 87 | ax.xaxis.set_major_locator(locator) |
| 88 | ax.xaxis.set_major_formatter(formatter) |
| 89 | ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2) |
| 90 | ax.set_xlabel(xlabel) |
| 91 | ax.set_ylabel(ylabel) |
| 92 | if include_legend: |
| 93 | ax.legend() |
| 94 | if not user_provided_ax: |
| 95 | fig.tight_layout() |
| 96 | return fig |
| 97 | |
| 98 | |
| 99 | def plot_components( |