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 obtenir les limites des axes dans Matplotlib (avec exemple)



Vous pouvez utiliser la syntaxe suivante pour obtenir les limites des axes pour les axes x et y d’un tracé dans Matplotlib :

import matplotlib.pyplot as plt

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt.axis()

#print axis limits
print(xmin, xmax, ymin, ymax)

L’exemple suivant montre comment utiliser cette syntaxe dans la pratique.

Exemple : Comment obtenir les limites des axes dans Matplotlib

Supposons que nous créions le nuage de points suivant dans Matplotlib :

import matplotlib.pyplot as plt

#define x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#create scatter plot of x vs. y
plt.scatter(x, y)

Nous pouvons utiliser la syntaxe suivante pour obtenir les limites des axes pour les axes x et y du nuage de points :

import matplotlib.pyplot as plt

#define x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#create scatter plot of x vs. y
plt.scatter(x, y)

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt.axis()

#print axis limits
print(xmin, xmax, ymin, ymax)

0.55 10.45 -1.0 43.0

À partir du résultat, nous pouvons voir :

  • Minimum sur l’axe des x : 0,55
  • Maximum sur l’axe des x : 10,45
  • Minimum sur l’axe y : -1,0
  • Maximum sur l’axe y : 43,0

Ces valeurs correspondent aux limites de l’axe visibles dans le nuage de points ci-dessus.

Nous pouvons également utiliser la fonction annotate() pour ajouter ces limites d’axe en tant que valeurs de texte au tracé si nous le souhaitons :

import matplotlib.pyplot as plt

#define x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#create scatter plot of x vs. y
plt.scatter(x, y)

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt.axis()

#print axis limits
lims = 'xmin: ' + str(round(xmin, 2)) + '\n' + \
       'xmax: ' + str(round(xmax, 2)) + '\n' + \
       'ymin: ' + str(round(ymin, 2)) + '\n' + \
       'ymax: ' + str(round(ymax, 2))

#add axis limits to plot at (x,y) coordinate (1,35)
plt.annotate(lims, (1, 35))

Matplotlib obtient les limites des axes

Ressources additionnelles

Les didacticiels suivants expliquent comment effectuer d’autres tâches courantes dans Matplotlib :

Comment définir les graduations d’axe dans Matplotlib
Comment augmenter la taille du tracé dans Matplotlib
Comment ajouter du texte aux tracés Matplotlib

Ajouter un commentaire

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