how to plot train and test together using matplotlib

Viewed 2131

I am trying to plot (y_train, y_test)and then (y_train_pred, y_test_pred) together in one gragh and i use the following code to do so

#plot 
plt.plot(y_test)
plt.plot(y_pred)
plt.plot(y_train)
plt.plot(train)
plt.legend(['y_train','y_train_pred', 'y_test', 'y_test_pred'])

Running the above gives me the below graph

enter image description here

But this isn't want i want. I want it in way that it's continous as show below (y_test continues/starts after y_train and y_pred starts after y_train_pred)

enter image description here

Any help will be great.

1 Answers

You can pass the x argument:

plt.plot(np.arange(len(y_pred)) + len(y_train),y_test)
plt.plot(np.arange(len(y_pred)) + len(y_train), y_pred)
plt.plot(y_train)
plt.plot(train)

# you seemed to mess up the labels
plt.legend(['y_test', 'y_test_pred', 'y_train','y_train_pred'])
Related