Hasani
Hasani

Reputation: 3889

How to plot my graphs in different windows with "matplotlib.pyplot"?

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?

Upvotes: 3

Views: 4046

Answers (3)

Manoj Kumar Biroj
Manoj Kumar Biroj

Reputation: 323

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()

Upvotes: 1

Manik Tharaka
Manik Tharaka

Reputation: 308

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()

Upvotes: 4

zabop
zabop

Reputation: 7912

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()

Upvotes: 3

Related Questions