Introduction matplotlib

Germain Salvato-Vallverdu germain.vallverdu@univ-pau.fr

Ce notebook est une très courte introduction à matplotlib. Le site de SciPy, propose un cours sur matplotlib. On trouve de plus une gallerie bien fournie sur le site de matplotlib d'où on peut extraire de nombreux exemples :

Dans IPython ou jupyter, on peut importer matplotlib avec la commande magique %pylab qui charge un environement de travail complet avec NumPy, SciPy et matplotlib.


In [68]:
%pylab --no-import-all inline


Populating the interactive namespace from numpy and matplotlib

Sinon on l'importe généralement en faisant :

import matplotlib.pyplot as plt

Exemples :

Ceci est un exemple pas à pas pour démarrer. Reportez-vous à la galerie pour plus d'exemples.

On va tracer la fonction $S(t)$ et son envelope :

$$ S(t) = \sin(2\,t) \,e^{-0.04\, t} $$

Définition des fonctions


In [87]:
def signal(t):
    return np.sin(2 * t) * np.exp(-4e-2 * t)
def envelope(t):
    return np.exp(-4e-2 * t)

Un premier graphique


In [70]:
t = np.linspace(0, 80, 200)
plt.plot(t, signal(t))
plt.savefig("/Users/gvallver/git/python_sciences/mpl/graph1.png")


On ajoute l'envelope


In [71]:
t = np.linspace(0, 80, 200)
plt.plot(t, signal(t))
plt.plot(t, envelope(t), color="red", linestyle="--")
plt.plot(t, -envelope(t), color="red", linestyle="--")
plt.savefig("/Users/gvallver/git/python_sciences/mpl/graph2.png")


 On ajoute des informations


In [72]:
t = np.linspace(0, 80, 200)
plt.plot(t, signal(t))
plt.plot(t, envelope(t), color="red", linestyle="--")
plt.plot(t, -envelope(t), color="red", linestyle="--")
plt.xlabel("temps (s)")
plt.ylabel("Signal (ua)")
plt.title("Ma super fonction amortie")
plt.savefig("/Users/gvallver/git/python_sciences/mpl/graph3.png")


Et la légende ?


In [73]:
t = np.linspace(0, 80, 200)
plt.plot(t, signal(t), label="signal")
plt.plot(t, envelope(t), color="red", linestyle="--", label="envelope")
plt.plot(t, -envelope(t), color="red", linestyle="--")
plt.xlabel("temps (s)")
plt.ylabel("Signal (ua)")
plt.title("Ma super fonction amortie")
plt.legend()
plt.savefig("/Users/gvallver/git/python_sciences/mpl/graph4.png")


Échelle des axes


In [77]:
t = np.linspace(0, 80, 200)
plt.plot(t, signal(t), label="signal")
plt.plot(t, envelope(t), color="red", linestyle="--", label="envelope")
plt.plot(t, -envelope(t), color="red", linestyle="--")
plt.xlabel("temps (s)")
plt.ylabel("Signal (ua)")
plt.title("Ma super fonction amortie")
plt.legend()
plt.xlim((0, 40))
plt.ylim((-1.2, 1.5))
plt.savefig("/Users/gvallver/git/python_sciences/mpl/graph5.png")


Une annotation


In [86]:
t = np.linspace(0, 80, 200)
plt.plot(t, signal(t), label="signal")
plt.plot(t, envelope(t), color="red", linestyle="--", label="envelope")
plt.plot(t, -envelope(t), color="red", linestyle="--")
plt.xlabel("temps (s)")
plt.ylabel("Signal (ua)")
plt.title("Ma super fonction amortie")
plt.legend()
plt.xlim((0, 40))
plt.ylim((-1.2, 1.5))
plt.scatter([25, ], [0, ], s=50)
plt.plot(x, 1 - 4e-2 * t, color="#0C0B0E", linestyle="solid", linewidth=.5)
plt.annotate("Vous avez vu là !!", color="#0C0B0E",
             xy=(25, 0), xycoords='data',
             xytext=(-40, +60), textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.8"))
plt.savefig("/Users/gvallver/git/python_sciences/mpl/graph6.png")


Et maintenant :

Maintenant, c'est à vous de jouer ! Il y a certainement un exemple dans la galerie qui se rapproche de se que vous voulez faire.