我有4个数组,p1&p2
和v1&v2
相似,我想在2个不同的窗口上绘制它们。我使用以下代码将它们全部绘制在一个窗口中,但如上所述,我想将它们分开:
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()
我应该如何更改代码?
答案 0 :(得分:2)
您可以在每次调用绘图之前致电plt.figure()
来实现此目的。
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()
答案 1 :(得分:1)
您应将代码用于plt.figure()
和plt.show()
之间的不同绘图,如下所示:
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()
答案 2 :(得分:1)
使用plt.subplot()将图形分为两个窗口。尝试以下代码,即可使用
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()