Home > Software design >  Graphing models on the same axis and plot
Graphing models on the same axis and plot

Time:12-27

I have a model of temperatures in Kelvin over a certain set of days. I am using a "Rolling Window" approach to model a linear regression for every 28 days, to predict temperatures for the next five days.

Now I have two graphs. How may I "put them together" so that the X-axis shows data from the entire set and not just a given window? The result would have two curves, one of real temperatures and one of predicted temperatures. The predicted model would not show for the first 28 days.

Two current graphs are show in the image below. Essentially, I wish to plot a model such that these graphs would appear next to each other in order. I am thinking this may be done in a for-loop but am unsure how to execute it.

from sklearn.linear_model import LinearRegression
import matplotlib
import matplotlib.pyplot as plt

reg = LinearRegression().fit(X[0:28], Y[0:28])
reg.score(X, Y)
Y_predict=reg.predict(X[28:33]) #data from days 29-33
plt.plot(Y[28:33],'r')
plt.plot(Y_predict,'b')
print(Y_predict) #temp, days 28-33
reg = LinearRegression().fit(X[29:57], Y[29:57])
Y_predict=reg.predict(X[57:62]) #data from days 29-33
plt.plot(Y[29:57],'r')
plt.plot(Y_predict,'b')
print(Y_predict) #temp, days 28-33

enter image description here

CodePudding user response:

To put the values together you can use the for-loop while saving all predicted values in a list, and only then execute the plot function. For that to work you have to adjust the fixed ranges in X and Y with the loop index, together with the WINDOW size and the amount of days to predict (NEXT_DAYS).

WINDOW = 28
NEXT_DAYS = 5

Y_predict = [None]*WINDOW
for i in range(0,len(X)-WINDOW, NEXT_DAYS):
    reg = LinearRegression().fit(X[i:i WINDOW], Y[i:i WINDOW])
    reg.score(X, Y)
    Y_predict.extend(reg.predict(X[i WINDOW:i WINDOW NEXT_DAYS]))

plt.plot(Y, 'r', label='Y', alpha=0.5)
plt.plot(Y_predict, 'b', label='Y_pred')
plt.legend()
plt.show()

multipart_plot

  • Related