#!/usr/bin/env python # coding: utf-8 # # Introduction matplotlib # # ##### Germain Salvato-Vallverdu [germain.vallverdu@univ-pau.fr](mailto: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 : # # * [Le cours sur SciPy](http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html) # * [La galerie sur matplotlib](http://matplotlib.org/gallery.html) # # 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]: get_ipython().run_line_magic('pylab', '--no-import-all inline') # Sinon on l'importe généralement en faisant : # # ```py # import matplotlib.pyplot as plt # ``` # ## Exemples : # # Ceci est un exemple pas à pas pour démarrer. Reportez-vous à # [la galerie](http://matplotlib.org/gallery.html) 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](http://matplotlib.org/gallery.html) qui se rapproche de se que vous voulez faire.