Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
matplotlib [Le 26/10/2017, 13:29] – ajout tag programmation et grapheur bcag2matplotlib [Le 18/10/2020, 21:05] (Version actuelle) – [Depuis les dépôts] 86.246.31.132
Ligne 1: Ligne 1:
 +{{tag>grapheurs programmation logiciels_pour_le_lycée calcul_numérique education}}
  
 +----
 +
 +====== Matplotlib ======
 +
 +**Matplotlib** est une bibliothèque Python permettant de réaliser des graphiques en 2D et 3D
 +
 +\\ 
 +
 +===== Pré-requis =====
 +
 +  * Disposer des [[:sudo|droits d'administration]].
 +  * Disposer d'une connexion à Internet configurée et activée.
 +
 +===== Installation =====
 +==== Depuis les dépôts ====
 +
 +[[:tutoriel:comment_installer_un_paquet|Installez le paquet]] **[[apt://python-matplotlib|matplotlib]]**.
 +
 +Ou par le terminal :
 +<code>
 +sudo apt-get install python-matplotlib</code>
 +
 +en python 3 :
 +<code>
 +sudo apt-get install python3-matplotlib</code>
 +===== Utilisation =====
 +
 +Exemple d'un graphique en 2D (tiré de [[http://commons.wikimedia.org/wiki/File:Logarithmic_Spiral_Pylab.svg?uselang=fr|Wikipédia]] (licence domaine public))
 +Créer un fichier et y coller le code :
 +<file python spirale.py>
 +#!/usr/bin/env python
 +from pylab import *
 +rc('grid', color='#aaaaaa', linewidth = 1, linestyle = '-')
 +figure(figsize = (6, 6))
 +ax = axes([0.1, 0.1, 0.8, 0.8], polar = True)
 +t = arange(-4 * pi, 4 * pi, .1)
 +polar(t, 1.19**t, linewidth = 2)
 +xt, yt = xticks()[0], yticks()[0]
 +xticks(xt, ['' for q in range(len(xt))])
 +yticks(yt, ['' for q in range(len(yt))])
 +savefig('logarithmic_spiral.jpg')
 +show()</file>
 +\\ 
 +Sauvegarder et rendre le fichier exécutable.\\
 +Lancer le code, par exemple en ligne de commande :<code> python spirale.py</code>
 +vous obtiendrez :
 +
 +{{ ::python:matplotlib:logarithmic_spiral.jpg ?direct&200 |}}
 +
 +Autre exemple d'utilisation (tiré de [[http://commons.wikimedia.org/wiki/File:Complex_arcsin_abs_01_Pengo.svg?uselang=fr|Wikipédia]] (licence domaine public)) :
 +Créer un fichier et y coller le code :
 +<file python graphs.py>
 +#!/usr/bin/env python
 + 
 +"""This generates four graphs for arcsin(z)."""
 +# Original: Peter Halasz. 2010-09-14
 +# Enhanced: Ika. 2013-07-23
 + 
 +import numpy as np
 +from pylab import *
 +from mpl_toolkits.mplot3d import Axes3D
 + 
 +graphs = {'abs':abs, 'real':real, 'imag':imag, 'angle':angle}
 + 
 +for gr in graphs:
 +        ax = Axes3D(figure(), azim = -135, elev = 45)
 +        X = arange(-2*pi, 2*pi, pi/12)
 +        Y = arange(-4, 4, .2)
 +        X, Y = meshgrid(X, Y)
 +        fn = graphs[gr]
 +        Z = fn(arcsin(X + Y*1j))  # abs, real, imag, angle. angle range [-pi, pi]
 +        ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap = cm.jet)
 +        ax.contour(X, Y, Z, zdir='z', offset=np.min(Z))
 +        ax.set_xlabel("x")
 +        ax.set_ylabel("y")
 +        ax.set_zlabel(gr + '(sin(x+iy))')
 +        plt.savefig("complex_arcsin_" + gr + "_01_Pengo.jpg")
 +</file>
 +\\ 
 +Sauvegarder et rendre le fichier exécutable.
 +Lancer le code, vous obtiendrez les figures suivantes :
 +\\ 
 +{{::python:matplotlib:complex_arcsin_abs_01_Pengo.jpg ?direct&200 |}}
 +{{ ::python:matplotlib:complex_arcsin_angle_01_Pengo.jpg?direct&200 |}}
 +{{::python:matplotlib:complex_arcsin_imag_01_Pengo.jpg ?direct&200 |}}
 +{{ ::python:matplotlib:complex_arcsin_real_01_Pengo.jpg?direct&200 |}}
 +
 +===== Voir aussi =====
 +
 +  * [[http://matplotlib.org/|site officiel matplotlib]]
 +  * [[Gnuplot]]
 +  * [[http://pyqtgraph.org/|PyQtGraph]]
 +
 +----
 +
 +//Contributeur : [[utilisateurs:G-Tux]].//