Mis a jour le 2024-03-17, 13:2

pyplot et généralités

Importation du module : from matplotlib import pyplot.
La taille par défaut d'une graphe est 6.4 x 4.8 (en inches) et 640 x 480 en pixels
Pour tracer un graphe x-y avec les points reliés (pour un nuage de points, utiliser plutôt scatter) :
Pour tracer un graphe d'une liste de valeurs en fonction du numéro d'ordre : pyplot.plot([1, 2, 4, 4, 2, 1], color = 'red', linestyle = 'dashed', linewidth = 2, markerfacecolor = 'blue', markersize = 5) pyplot.ylim(0, 5) pyplot.title('Un exemple')
On peut aussi tracer seulement les points, non reliés : pyplot.plot([1, 3, 2, 3], [4, 8, 5, 4], linestyle = 'none', marker = 'o', c = 'lime', markersize = 10) pyplot.xlim(0, 4) pyplot.ylim(0, 10) pyplot.title('Avec des points seulement')
Si on veut mettre des etiquettes tous les n valeurs : import math val = [math.log(i + 1) for i in range(100)] labels = ['lab' + str(i) for i in range(100)] tickLabels = [labels[i] for i in range(100) if (i % 10) == 0] pyplot.plot(val, linestyle = 'none', marker = '+') pyplot.gca().get_xaxis().set_ticklabels(tickLabels, fontsize = 8, rotation = 90) pyplot.gca().get_xaxis().set_ticks([i for i in range(len(val)) if i % 10 == 0])
Remplissage de la zone entre 2 courbes : x = range(10); y1 = range(10) y2 = [x ** 2 for x in range(10)] pyplot.fill_between(x, y1, y2, color = 'yellow')
Manipulation d'un graphe :
Sauvegarde de l'image dans un fichier :
Si on veut faire des graphes indépendamment d'un environnement X : faire :
import matplotlib
matplotlib.use('agg')
  
avant d'importer pyplot. Cela permet de fixer le backend par défaut (on peut voir le backend par défaut en faisant : matplotlib.get_backend()). Il faut que l'instruction matplotlib.use soit appelée avant toute tentative d'utilisation de pyplot.
Réinitalisation / effaçage :
Pour fixer la taille d'une figure :
Pour indiquer une propriété :
Graduations des axes :
Figure et axe courants :
Traçage de lignes en dehors de la plage graduée :
Ajout des titres et différents labels dans une figure :
Exemple : pyplot.scatter([x / 2.0 for x in range(10)], [(x / 2.0) ** 2 for x in range(10)], color = 'blue', marker = 'D', s = 30) pyplot.xlabel('coordonnee X') pyplot.ylabel('coordonnee Y') pyplot.title('mon titre') pyplot.text(0, 20, 'parabole') pyplot.annotate('annotation', xy = (0, 0), xytext = (1, 10), arrowprops = {'facecolor': 'red', 'shrink': 0.1}) pyplot.grid()
Propriétés par défaut :
Pour changer le cycle des couleurs par défaut : pyplot.rcParams['axes.prop_cycle'] = plt.cycler(color=['orange', 'blue', 'green', 'red', 'purple', 'brown', 'pink', 'gray', 'olive', 'cyan'])
couleurs et symboles des graphes :
Axes :
Traçage de droites horizontales ou verticales : exemple : pyplot.plot([1, 2, 4, 6, 7, 8, 10, 15]) pyplot.axhline(y = 3, xmin = 0, xmax = 1, color = 'red', alpha = 0.5, linestyle = ':', linewidth = 2) pyplot.axvline(x = 4, ymin = 0, ymax = 0.8, color = 'green', alpha = 0.5, linestyle = '--', linewidth = 4)
Traçage d'une droite :
Traçages de rectangles :
matrice :
Légende :
zorder :
Menu interactif :
si on travaille avec matplotlib depuis plusieurs threads, utiliser un backend non interactif : import matplotlib; matplotlib.use('Agg')
Quand on utilise matplot sur un serveur web :

Copyright python-simple.com
programmer en python, tutoriel python, graphes en python, Aymeric Duclert