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 dessiner une ligne verticale dans Matplotlib (avec exemples)



Vous pouvez utiliser la syntaxe suivante pour tracer une ligne verticale dans Matplotlib :

import matplotlib.pyplot as plt

#draw vertical line at x=2
plt.axvline(x=2)

Les exemples suivants montrent comment utiliser cette syntaxe en pratique avec le DataFrame pandas suivant :

import pandas as pd

#create DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8],
                   'y': [5, 7, 8, 15, 26, 39, 45, 40]})

#view DataFrame
df

	x	y
0	1	5
1	2	7
2	3	8
3	4	15
4	5	26
5	6	39
6	7	45
7	8	40

Exemple 1 : tracer une ligne verticale

Le code suivant montre comment tracer une ligne verticale sur un tracé Matplotlib :

import matplotlib.pyplot as plt

#create line plot
plt.plot(df.x, df.y)

#add vertical line at x=2
plt.axvline(x=2, color='red', linestyle='--')

Tracez une ligne verticale dans Matplotlib

Exemple 2 : tracer plusieurs lignes verticales

Le code suivant montre comment dessiner plusieurs lignes verticales sur un tracé Matplotlib :

import matplotlib.pyplot as plt

#create line plot
plt.plot(df.x, df.y)

#add vertical line at x=2
plt.axvline(x=2, color='red', linestyle='--')

#add vertical line at x=4
plt.axvline(x=4, color='black', linestyle='-')

Tracer plusieurs lignes dans Matplotlib

Exemple 3 : tracer plusieurs lignes verticales avec une légende

Le code suivant montre comment dessiner plusieurs lignes verticales sur un tracé Matplotlib et ajouter une légende pour faciliter l’interprétation des lignes :

import matplotlib.pyplot as plt

#create line plot
plt.plot(df.x, df.y)

#add vertical line at x=2
plt.axvline(x=2, color='red', linestyle='--', label='First Line')

#add vertical line at x=4
plt.axvline(x=4, color='black', linestyle='-', label='Second Line')

#add legend
plt.legend()

Matplotlib plusieurs lignes verticales avec légende

Remarque : reportez-vous à la documentation Matplotlib pour obtenir une liste des couleurs et des styles de ligne potentiels que vous pouvez appliquer aux lignes verticales.

Ressources additionnelles

Comment dessiner une ligne horizontale dans Matplotlib
Comment tracer plusieurs lignes dans Matplotlib
Comment tracer une série chronologique dans Matplotlib
Comment dessiner des rectangles dans Matplotlib
Comment dessiner des flèches dans Matplotlib

Ajouter un commentaire

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