2
votes

Comment tracer mes graphiques dans différentes fenêtres avec "matplotlib.pyplot"?

J'ai 4 tableaux que p1 & p2 et v1 & v2 sont similaires et j'aime les tracer sur 2 fenêtres différentes. J'utilise le code suivant pour tous les tracer dans une fenêtre, mais j'aime les séparer comme je l'ai dit ci-dessus:

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

Comment changer mon code?


1 commentaires

Utilisez pyplot.figure () pour chaque tracé que vous souhaitez afficher dans une fenêtre séparée


3 Réponses :


1
votes

Vous devez placer le code des différents tracés entre plt.figure () et plt.show () comme suit:

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

p>


0 commentaires

3
votes

Vous pouvez appeler plt.figure () avant chaque appel de tracé pour y parvenir.

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


2 commentaires

Il a demandé seulement 2 fenêtres pas 4


@Manojbiroj: votre réponse crée également une seule fenêtre et non deux bro :)



1
votes

utilisez plt.subplot () pour diviser le graphique en deux fenêtres. Essayez le code ci-dessous, cela fonctionnera

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


0 commentaires