Personnaliser les préférences

Nous utilisons des cookies pour vous aider à naviguer efficacement et à exécuter certaines fonctions. Vous trouverez ci-dessous des informations détaillées sur tous les cookies sous chaque catégorie de consentement.

Les cookies classés comme « Nécessaires » sont stockés sur votre navigateur car ils sont essentiels pour activer les fonctionnalités de base du site.... 

Toujours actif

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Aucun cookie à afficher.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Aucun cookie à afficher.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Aucun cookie à afficher.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Aucun cookie à afficher.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

Aucun cookie à afficher.

Comment créer plusieurs tracés Matplotlib sur une seule figure



Vous pouvez utiliser la syntaxe suivante pour créer plusieurs tracés Matplotlib dans une seule figure :

import matplotlib.pyplot as plt

#define grid of plots
fig, axs = plt.subplots(nrows=2, ncols=1)

#add data to plots
axs[0].plot(variable1, variable2)
axs[1].plot(variable3, variable4)

Les exemples suivants montrent comment utiliser cette fonction dans la pratique.

Exemple 1 : empiler des tracés verticalement

Le code suivant montre comment créer trois tracés Matplotlib, empilés verticalement :

#create some data
var1 = [1, 2, 3, 4, 5, 6]
var2 = [7, 13, 16, 18, 25, 19]
var3 = [29, 25, 20, 25, 20, 18]

#define grid of plots
fig, axs = plt.subplots(nrows=3, ncols=1)

#add title
fig.suptitle('Plots Stacked Vertically')

#add data to plots
axs[0].plot(var1, var2)
axs[1].plot(var1, var3)
axs[2].plot(var2, var3)

Plusieurs tracés empilés verticalement dans Matplotlib

Exemple 2 : empiler des tracés horizontalement

Le code suivant montre comment créer trois tracés Matplotlib, empilés horizontalement :

#create some data
var1 = [1, 2, 3, 4, 5, 6]
var2 = [7, 13, 16, 18, 25, 19]
var3 = [29, 25, 20, 25, 20, 18]

#define grid of plots
fig, axs = plt.subplots(nrows=1, ncols=3)

#add title
fig.suptitle('Plots Stacked Horizontally')

#add data to plots
axs[0].plot(var1, var2)
axs[1].plot(var1, var3)
axs[2].plot(var2, var3)

Plusieurs tracés Matplotlib empilés horizontalement

Exemple 3 : Créer une grille de tracés

Le code suivant montre comment créer une grille de tracés Matplotlib :

#create some data
var1 = [1, 2, 3, 4, 5, 6]
var2 = [7, 13, 16, 18, 25, 19]
var3 = [29, 25, 20, 25, 20, 18]
var4 = [4, 4, 6, 4, 7, 11]

#define grid of plots
fig, axs = plt.subplots(nrows=2, ncols=2)

#add title
fig.suptitle('Grid of Plots')

#add data to plots
axs[0, 0].plot(var1, var2)
axs[0, 1].plot(var1, var3)
axs[1, 0].plot(var1, var4)
axs[1, 1].plot(var3, var1)

Plusieurs tracés dans Matplotlib

Exemple 4 : Partager des axes entre des parcelles

Vous pouvez utiliser les arguments sharex et sharey pour garantir que plusieurs tracés utilisent le même axe x :

#create some data
var1 = [1, 2, 3, 4, 5, 6]
var2 = [7, 13, 16, 18, 25, 19]
var3 = [29, 25, 20, 25, 20, 18]
var4 = [4, 4, 6, 4, 7, 11]

#define grid of plots
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)

#add title
fig.suptitle('Grid of Plots with Same Axes')

#add data to plots
axs[0, 0].plot(var1, var2)
axs[0, 1].plot(var1, var3)
axs[1, 0].plot(var1, var4)
axs[1, 1].plot(var3, var1)

Plusieurs tracés dans Matplotlib avec des axes partagés

Ressources additionnelles

Comment ajuster l’espacement entre les sous-parcelles Matplotlib
Comment changer la couleur d’arrière-plan dans Matplotlib
Comment augmenter la taille du tracé dans Matplotlib

Ajouter un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *