Matplotlib Subplot
Matplotlib Subplot
Exemple_1
Afficher plusieurs tracés
Avec la fonction subplot() vous pouvez dessiner plusieurs tracés sur une
seule figure :
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
Hammadi Oumaima
Matplotlib Subplot LSE3
Exemple_2 :
Ainsi, si nous voulons une figure avec 2 lignes et 1 colonne (ce qui signifie
que les deux tracés seront affichés l'un sur l'autre au lieu de côte à côte),
nous pouvons écrire la syntaxe comme celle-ci :
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 1, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 1, 2)
plt.plot(x,y)
plt.show()
Exemple_3:
Vous pouvez dessiner autant de tracés que vous le souhaitez sur une seule
figure, décrivez simplement le nombre de lignes, de colonnes et l'index du
tracé.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 2, 1)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 2, 2)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
Hammadi Oumaima
Matplotlib Subplot LSE3
plt.subplot(2, 2, 3)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 2, 4)
plt.plot(x,y)
plt.show()
Exemple_4:
*Titre
Vous pouvez ajouter un titre à chaque tracé avec la fonction title() :
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("VENTES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("REVENU")
plt.show()
Exemple_5 :
*sous-titre
Vous pouvez ajouter un titre à la figure entière avec la fonction suptitle() :
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
Hammadi Oumaima
Matplotlib Subplot LSE3
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("VENTES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("REVENU")
plt.suptitle("MON MAGASIN")
plt.show()
Hammadi Oumaima