Plot
Forecast Zoom-in
Code:
def forecast_plotting(model, forecast, future, forecast_length):
fig = model.plot(forecast)
ax = fig.gca()
cutoff = date2num(np.array(future.iloc[-forecast_length]["ds"]))
lines = ax.get_lines()
forecast_line = lines[1]
x_data, y_data = forecast_line.get_data()
x_data_num = date2num(x_data)
forecast_start_index = x_data_num.searchsorted(cutoff)
ax.plot(
x_data[forecast_start_index:],
y_data[forecast_start_index:],
color="orange",
linewidth=2,
)
ax.plot(
x_data[forecast_start_index:],
forecast["yhat_lower"][forecast_start_index:],
color="red",
linewidth=2
)
ax.plot(
x_data[forecast_start_index:],
forecast["yhat_upper"][forecast_start_index:],
color="red",
linewidth=2
)
# Fill the area between the red lines with a light red color
ax.fill_between(
x_data[forecast_start_index:],
forecast["yhat_lower"][forecast_start_index:],
forecast["yhat_upper"][forecast_start_index:],
color="red",
alpha=0.3, # Set transparency for better visualization
)
return fig
The input parameters for this functions are mostly returned by the Prophet model which Im using.
So the “future” is a dataframe with dates:
future param
the forecast is also returned by the Prophet model:
forecast param
Question:
As you can see, the upper and lower bounds are plotted so the index and data are valid. The problem is that I, for the love of god, cannot get the fill_between function to work. Basically I want to also visualize the upper and lower bounds of my predictions. The plotted red lines are just there for clarification and the end result should just be a orange-out area behind the predicted line (orange) to clearly show the upper and lower areas.
I’ve tried some smoothing of the bounds so that the area is quite simple, but it did not work!
Deni Cuturic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.