Module: Prophet::Plot

Included in:
Forecaster
Defined in:
lib/prophet/plot.rb

Instance Method Summary collapse

Instance Method Details

#add_changepoints_to_plot(ax, fcst, threshold: 0.01, cp_color: "r", cp_linestyle: "--", trend: true) ⇒ Object

in Python, this is a separate method



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/prophet/plot.rb', line 97

def add_changepoints_to_plot(ax, fcst, threshold: 0.01, cp_color: "r", cp_linestyle: "--", trend: true)
  artists = []
  if trend
    artists << ax.plot(to_pydatetime(fcst["ds"]), fcst["trend"].to_a, c: cp_color)
  end
  signif_changepoints =
    if @changepoints.size > 0
      (@params["delta"].mean(axis: 0, nan: true).abs >= threshold).mask(@changepoints.to_numo)
    else
      []
    end
  to_pydatetime(signif_changepoints).each do |cp|
    artists << ax.axvline(x: cp, c: cp_color, ls: cp_linestyle)
  end
  artists
end

#plot(fcst, ax: nil, uncertainty: true, plot_cap: true, xlabel: "ds", ylabel: "y", figsize: [10, 6]) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/prophet/plot.rb', line 3

def plot(fcst, ax: nil, uncertainty: true, plot_cap: true, xlabel: "ds", ylabel: "y", figsize: [10, 6])
  if ax.nil?
    fig = plt.figure(facecolor: "w", figsize: figsize)
    ax = fig.add_subplot(111)
  else
    fig = ax.get_figure
  end
  fcst_t = to_pydatetime(fcst["ds"])
  ax.plot(to_pydatetime(@history["ds"]), @history["y"].to_a, "k.")
  ax.plot(fcst_t, fcst["yhat"].to_a, ls: "-", c: "#0072B2")
  if fcst.include?("cap") && plot_cap
    ax.plot(fcst_t, fcst["cap"].to_a, ls: "--", c: "k")
  end
  if @logistic_floor && fcst.include?("floor") && plot_cap
    ax.plot(fcst_t, fcst["floor"].to_a, ls: "--", c: "k")
  end
  if uncertainty && @uncertainty_samples
    ax.fill_between(fcst_t, fcst["yhat_lower"].to_a, fcst["yhat_upper"].to_a, color: "#0072B2", alpha: 0.2)
  end
  # Specify formatting to workaround matplotlib issue #12925
  locator = dates.AutoDateLocator.new(interval_multiples: false)
  formatter = dates.AutoDateFormatter.new(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
  fig
end

#plot_components(fcst, uncertainty: true, plot_cap: true, weekly_start: 0, yearly_start: 0, figsize: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/prophet/plot.rb', line 34

def plot_components(fcst, uncertainty: true, plot_cap: true, weekly_start: 0, yearly_start: 0, figsize: nil)
  components = ["trend"]
  if @train_holiday_names && fcst.include?("holidays")
    components << "holidays"
  end
  # Plot weekly seasonality, if present
  if @seasonalities["weekly"] && fcst.include?("weekly")
    components << "weekly"
  end
  # Yearly if present
  if @seasonalities["yearly"] && fcst.include?("yearly")
    components << "yearly"
  end
  # Other seasonalities
  components.concat(@seasonalities.keys.select { |name| fcst.include?(name) && !["weekly", "yearly"].include?(name) }.sort)
  regressors = {"additive" => false, "multiplicative" => false}
  @extra_regressors.each do |name, props|
    regressors[props[:mode]] = true
  end
  ["additive", "multiplicative"].each do |mode|
    if regressors[mode] && fcst.include?("extra_regressors_#{mode}")
      components << "extra_regressors_#{mode}"
    end
  end
  npanel = components.size

  figsize = figsize || [9, 3 * npanel]
  fig, axes = plt.subplots(npanel, 1, facecolor: "w", figsize: figsize)

  if npanel == 1
    axes = [axes]
  end

  multiplicative_axes = []

  axes.tolist.zip(components) do |ax, plot_name|
    if plot_name == "trend"
      plot_forecast_component(fcst, "trend", ax: ax, uncertainty: uncertainty, plot_cap: plot_cap)
    elsif @seasonalities[plot_name]
      if plot_name == "weekly" || @seasonalities[plot_name][:period] == 7
        plot_weekly(name: plot_name, ax: ax, uncertainty: uncertainty, weekly_start: weekly_start)
      elsif plot_name == "yearly" || @seasonalities[plot_name][:period] == 365.25
        plot_yearly(name: plot_name, ax: ax, uncertainty: uncertainty, yearly_start: yearly_start)
      else
        plot_seasonality(name: plot_name, ax: ax, uncertainty: uncertainty)
      end
    elsif ["holidays", "extra_regressors_additive", "extra_regressors_multiplicative"].include?(plot_name)
      plot_forecast_component(fcst, plot_name, ax: ax, uncertainty: uncertainty, plot_cap: false)
    end
    if @component_modes["multiplicative"].include?(plot_name)
      multiplicative_axes << ax
    end
  end

  fig.tight_layout
  # Reset multiplicative axes labels after tight_layout adjustment
  multiplicative_axes.each do |ax|
    ax = set_y_as_percent(ax)
  end
  fig
end