Tracé de courbes Pour tracer des courbes, Python n’est pas suffisant et nous av

Tracé de courbes Pour tracer des courbes, Python n’est pas suffisant et nous avons besoin des librairies scientifiques NumPy et Matplotlib utilisées dans ce cours. Si vous ne disposez pas de ces librairies, vous pouvez consulter la page Introduction à Python (introduction-python.html) pour installer l’environnement adapté à ce cours. Dans cette page, nous présentons deux syntaxes : la syntaxe “PyLab” qui est proche de celle de Matlab et la syntaxe “standard” qui est recommandée dans les nouvelles versions de Matplotlib. Pour la syntaxe “PyLab”, il suffit de faire : from pylab import * Il est alors possible d’accéder directement aux fonctions de NumPy et Matplotlib. Pour la syntaxe “standard”, il faut importer NumPy et le module pyplot de Matplotlib. On doit alors préciser les librairies lors des appels des fonctions (voir les exemples ci-dessous). Pour en savoir plus sur la notion d’importation, vous pouvez consulter la page Modules et importations (modules.html). Création d’une courbe Utilisation de plot() L’instruction plot() permet de tracer des courbes qui relient des points dont les abscisses et ordonnées sont fournies dans des tableaux. Exemple 1 Syntaxe “PyLab” from pylab import * x = array([1, 3, 4, 6]) y = array([2, 3, 5, 1]) plot(x, y) show() # affiche la figure a l'ecran Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.array([1, 3, 4, 6]) y = np.array([2, 3, 5, 1]) plt.plot(x, y) plt.show() # affiche la figure a l'ecran (Source code (./courbes/courbe0a.py)) Exemple 2 Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y = cos(x) plot(x, y) show() # affiche la figure a l'ecran Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y = np.cos(x) plt.plot(x, y) plt.show() # affiche la figure a l'ecran (Source code (./courbes/courbe1a.py)) Définition du domaine des axes - xlim() et ylim() Il est possible fixer indépendamment les domaines des abscisses et des ordonnées en utilisant les fonctions xlim() et ylim(). xlim(xmin, xmax) ylim(ymin, ymax) Exemple 1 Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y = cos(x) plot(x, y) xlim(‐1, 5) show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y = np.cos(x) plt.plot(x, y) plt.xlim(‐1, 5) plt.show() (Source code (./courbes/courbe11a.py)) Exemple 2 Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y = cos(x) plot(x, y) ylim(‐2, 2) show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y = np.cos(x) plt.plot(x, y) plt.ylim(‐2, 2) plt.show() (Source code (./courbes/courbe12a.py)) Exemple 3 Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y = cos(x) plot(x, y) xlim(0, 2*pi) ylim(‐2, 2) show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y = np.cos(x) plt.plot(x, y) plt.xlim(0, 2*np.pi) plt.ylim(‐2, 2) plt.show() (Source code (./courbes/courbe10a.py)) Ajout d’un titre - title() On peut ajouter un titre grâce à l’instruction title(). Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y = cos(x) plot(x, y) title("Fonction cosinus") show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y = np.cos(x) plt.plot(x, y) plt.title("Fonction cosinus") plt.show() (Source code (./courbes/courbe13a.py)) Ajout d’une légende - legend() Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y = cos(x) plot(x, y, label="cos(x)") legend() show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y = np.cos(x) plt.plot(x, y, label="cos(x)") plt.legend() plt.show() (Source code (./courbes/courbe3a.py)) Warning Pour faire afficher le label, il ne faut pas oublier de faire appel à l’instruction legend(). Labels sur les axes - xlabel() et ylabel() Des labels sur les axes peuvent être ajoutés avec les fonctions xlabel() et ylabel(). Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y = cos(x) plot(x, y) xlabel("abscisses") ylabel("ordonnees") show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y = np.cos(x) plt.plot(x, y) plt.xlabel("abscisses") plt.ylabel("ordonnees") plt.show() (Source code (./courbes/courbe14a.py)) Affichage de plusieurs courbes Pour afficher plusieurs courbes sur un même graphe, on peut procéder de la façon suivante : Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y1 = cos(x) y2 = sin(x) plot(x, y1) plot(x, y2) show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y1 = np.cos(x) y2 = np.sin(x) plt.plot(x, y1) plt.plot(x, y2) plt.show() (Source code (./courbes/courbe5a.py)) Exemple avec un légende Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y1 = cos(x) y2 = sin(x) plot(x, y1, label="cos(x)") plot(x, y2, label="sin(x)") legend() show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y1 = np.cos(x) y2 = np.sin(x) plt.plot(x, y1, label="cos(x)") plt.plot(x, y2, label="sin(x)") plt.legend() plt.show() (Source code (./courbes/courbe6a.py)) Formats de courbes Il est possible de préciser la couleur, le style de ligne et de symbole (“marker”) en ajoutant une chaîne de caractères de la façon suivante : Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y1 = cos(x) y2 = sin(x) plot(x, y1, "r‐‐", label="cos(x)") plot(x, y2, "b:o", label="sin(x)") legend() show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y1 = np.cos(x) y2 = np.sin(x) plt.plot(x, y1, "r‐‐", label="cos(x)") plt.plot(x, y2, "b:o", label="sin(x)") plt.legend() plt.show() (Source code (./courbes/courbe7a.py)) Style de ligne Les chaînes de caractères suivantes permettent de définir le style de ligne : Chaîne Effet ‐ ligne continue ‐‐ tirets : ligne en pointillé ‐. tirets points Warning Si on ne veut pas faire apparaître de ligne, il suffit d’indiquer un symbole sans préciser un style de ligne. Exemple Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 20) y = sin(x) plot(x, y, "o‐", label="ligne ‐") plot(x, y‐0.5, "o‐‐", label="ligne ‐‐") plot(x, y‐1, "o:", label="ligne :") plot(x, y‐1.5, "o‐.", label="ligne ‐.") plot(x, y‐2, "o", label="pas de ligne") legend() show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 20) y = np.sin(x) plt.plot(x, y, "o‐", label="ligne ‐") plt.plot(x, y‐0.5, "o‐‐", label="ligne ‐‐") plt.plot(x, y‐1, "o:", label="ligne :") plt.plot(x, y‐1.5, "o‐.", label="ligne ‐.") plt.plot(x, y‐2, "o", label="pas de ligne") plt.legend() plt.show() (Source code (./courbes/courbe21a.py)) Symbole (“marker”) Les chaînes de caractères suivantes permettent de définir le symbole (“marker”) : Chaîne Effet . point marker , pixel marker o circle marker v triangle_down marker ^ triangle_up marker < triangle_left marker > triangle_right marker Couleur Les chaînes de caractères suivantes permettent de définir la couleur : Chaîne Couleur en anglais Couleur en français b blue bleu g green vert r red rouge c cyan cyan m magenta magenta y yellow jaune k black noir 1 tri_down marker 2 tri_up marker 3 tri_left marker 4 tri_right marker s square marker p pentagon marker * star marker h hexagon1 marker H hexagon2 marker + plus marker x x marker D diamond marker d thin_diamond marker | vline marker _ hline marker w white blanc Largeur de ligne Pour modifier la largeur des lignes, il est possible de changer la valeur de l’argument linewidth . Syntaxe “PyLab” from pylab import * x = linspace(0, 2*pi, 30) y1 = cos(x) y2 = sin(x) plot(x, y1, label="cos(x)") plot(x, y2, label="sin(x)", linewidth=4) legend() show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 30) y1 = np.cos(x) y2 = np.sin(x) plt.plot(x, y1, label="cos(x)") plt.plot(x, y2, label="sin(x)", linewidth=4) plt.legend() plt.show() (Source code (./courbes/courbe8a.py)) Tracé de formes Comme la fonction plot() ne fait que relier des points, il est possible de lui fournir plusieurs points avec la même abscisse. Syntaxe “PyLab” from pylab import * x = array([0, 1, 1, 0, 0]) y = array([0, 0, 1, 1, 0]) plot(x, y) xlim(‐1, 2) ylim(‐1, 2) show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.array([0, 1, 1, 0, 0]) y = np.array([0, 0, 1, 1, 0]) plt.plot(x, y) plt.xlim(‐1, 2) plt.ylim(‐1, 2) plt.show() (Source code (./courbes/courbe15a.py)) L’instruction axis("equal") L’instruction axis("equal") permet d’avoir la même échelle sur l’axe des abscisses et l’axe des ordonnées afin de préserver la forme lors de l’affichage. En particulier, grâce à cette commande un carré apparaît vraiment comme un carré, de même pour un cercle. Syntaxe “PyLab” from pylab import * x = array([0, 1, 1, 0, 0]) y = array([0, 0, 1, 1, 0]) plot(x, y) axis("equal") show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.array([0, 1, 1, 0, 0]) y = np.array([0, 0, 1, 1, 0]) plt.plot(x, y) plt.axis("equal") plt.show() (Source code (./courbes/courbe16a_equal.py)) Il est aussi possible de fixer le domaine des abscisses en utilisant xlim(). Syntaxe “PyLab” from pylab import * x = array([0, 1, 1, 0, 0]) y = array([0, 0, 1, 1, 0]) plot(x, y) axis("equal") xlim(‐1, 2) show() Syntaxe “standard” import matplotlib.pyplot as plt import numpy as np x = np.array([0, 1, 1, 0, 0]) y = np.array([0, 0, 1, 1, 0]) plt.plot(x, y) plt.axis("equal") plt.xlim(‐1, 2) plt.show() (Source code (./courbes/courbe17a_equal.py)) Tracé d’un cercle On peut tracer utiliser une courbe uploads/S4/ trace-de-courbes-courspython.pdf

  • 24
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager
  • Détails
  • Publié le Apv 01, 2021
  • Catégorie Law / Droit
  • Langue French
  • Taille du fichier 0.4557MB