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 des flèches dans Matplotlib



Pour dessiner des flèches dans Matplotlib, vous pouvez utiliser la fonction matplotlib.pyplot.arrow , qui utilise la syntaxe suivante :

matplotlib.pyplot.arrow(x, y, dx, dy)

où:

  • x, y : les coordonnées x et y de la base de la flèche
  • dx, dy : la longueur de la flèche dans les directions x et y

Ce tutoriel fournit plusieurs exemples d’utilisation pratique de cette fonction.

Exemple 1 : dessiner une seule flèche

Le code suivant montre comment dessiner une seule flèche sur un tracé Matplotlib :

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#create scatterplot, specifying marker size to be 40
plt.scatter(A, B, s=40)

#add arrow to plot
plt.arrow(x=4, y=18, dx=2, dy=5, width=.08) 
  
#display plot 
plt.show()

Flèche dans le tracé matplotlib

Notez que nous pouvons définir dx=0 pour créer une flèche verticale et dy=0 pour créer une flèche horizontale.

Par exemple, voici comment créer une flèche verticale :

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#create scatterplot, specifying marker size to be 40
plt.scatter(A, B, s=40)

#add arrow to plot
plt.arrow(x=4, y=18, dx=0, dy=5, width=.08) 
  
#display plot 
plt.show()

Flèche verticale dans matplotlib

Exemple 2 : styliser une flèche

Par défaut, une flèche dans Matplotlib est bleue avec des bords noirs mais nous pouvons facilement changer cela en utilisant les arguments facecolor et edgecolor :

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#create scatterplot, specifying marker size to be 40
plt.scatter(A, B, s=40)

#add arrow to plot
plt.arrow(x=4, y=18, dx=0, dy=5, width=.08, facecolor='red', edgecolor='none') 
  
#display plot 
plt.show()

Flèche avec couleur personnalisée dans matplotlib

Vous pouvez trouver une liste complète des propriétés de style pouvant être appliquées aux flèches ici .

Exemple 3 : ajouter des annotations aux flèches

Le code suivant montre comment ajouter une annotation sous une flèche sur un tracé Matplotlib :

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#create scatterplot, specifying marker size to be 40
plt.scatter(A, B, s=40)

#add arrow to plot
plt.arrow(x=4, y=18, dx=0, dy=5, width=.08) 

#add annotation
plt.annotate('General direction', xy = (3.4, 17))
  
#display plot 
plt.show()

Flèche avec annotation dans matplotlib

Ressources additionnelles

Comment tracer des cercles dans Matplotlib (avec exemples)
Comment dessiner des rectangles dans Matplotlib (avec exemples)

Ajouter un commentaire

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