3

I have 4 arrays that p1&p2 and v1&v2 are similar and I like to plotting them on 2 different windows. I use the following code to plot all of them in 1 window, but like to separate them as I said above:

p1 = real_stock_price_volume[:,0]
v1 = real_stock_price_volume[:,1]
p2 = predicted_stock_price_volume[:,0]
v2 = predicted_stock_price_volume[:,1]
plt.plot(p1, color = 'red', label = 'p1')
plt.plot(v1, color = 'brown', label = 'v1')
plt.plot(p2, color = 'blue', label = 'p2')
plt.plot(v2, color = 'green', label = 'v2')
plt.title('Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()

How should I change my code?

1
  • 1
    Use pyplot.figure() for every plot you want to display in a separate window Commented Feb 25, 2019 at 11:37

3 Answers 3

4

You can call plt.figure() before each plot call to achieve this.

p1 = real_stock_price_volume[:,0]
v1 = real_stock_price_volume[:,1]
p2 = predicted_stock_price_volume[:,0]
v2 = predicted_stock_price_volume[:,1]

plt.figure(1)
plt.plot(p1, color = 'red', label = 'p1')
plt.title('Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')

plt.figure(2)
plt.plot(v1, color = 'brown', label = 'v1')
plt.title('Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')

plt.figure(3)
plt.plot(p2, color = 'blue', label = 'p2')
plt.title('Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')

plt.figure(4)
plt.plot(v2, color = 'green', label = 'v2')
plt.title('Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()

plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

He asked for only 2 windows not for 4
@Manojbiroj: your answer also creates only 1 window not two bro :)
3

You should put the code for different plots between plt.figure() and plt.show() as follows:

p1 = real_stock_price_volume[:,0]
v1 = real_stock_price_volume[:,1]
p2 = predicted_stock_price_volume[:,0]
v2 = predicted_stock_price_volume[:,1]

plt.figure()
plt.plot(p1, color = 'red', label = 'p1')
# you can add other instrunctions here, such as title, xlabel, etc
plt.show()

plt.figure()
plt.plot(v1, color = 'brown', label = 'v1')
# you can add other instrunctions here, such as title, xlabel, etc
plt.show()

plt.figure()
plt.plot(p2, color = 'blue', label = 'p2')
# you can add other instrunctions here, such as title, xlabel, etc
plt.show()

Comments

1

use plt.subplot() for splitting the graph into two windows.Try the below code it will work

plt.subplot(121)
plt.plot(p1, color = 'red', label = 'p1')
plt.plot(v1, color = 'blue', label = 'v1')
plt.title('real Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.subplot(122)
plt.plot(p2, color = 'brown', label = 'p2')
plt.plot(v2, color = 'green', label = 'v2')
plt.title('Predicted Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.