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 tracer uniquement un quadrillage horizontal dans Matplotlib



Vous pouvez utiliser la syntaxe de base suivante pour tracer uniquement un quadrillage horizontal dans Matplotlib :

ax.grid(axis='y')

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

Exemple : tracer uniquement un quadrillage horizontal dans Matplotlib

Le code suivant montre comment créer un tracé à barres dans Matplotlib avec uniquement un quadrillage horizontal affiché dans le tracé :

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines
ax.grid(axis='y')

#display plot
plt.show()

N’hésitez pas à utiliser ax.set_axisbelow(True) pour afficher le quadrillage horizontal derrière les barres dans le tracé :

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines behind bars in the plot
ax.set_axisbelow(True)
ax.grid(axis='y')

#display plot
plt.show()

Quadrillage horizontal Matplotlib

Et n’hésitez pas à utiliser les arguments color , linestyle et linewidth dans la fonction grid() pour personnaliser l’apparence du quadrillage :

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines with custom appearance
ax.set_axisbelow(True)
ax.grid(axis='y', color='red', linestyle='dashed', linewidth=3)

#display plot
plt.show()

Vous pouvez trouver une liste complète des façons de personnaliser le quadrillage dans la documentation Matplotlib .

Ressources additionnelles

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

Comment supprimer les tiques des tracés Matplotlib
Comment modifier la taille des polices sur un tracé Matplotlib
Comment ajouter une ligne moyenne au tracé dans Matplotlib

Ajouter un commentaire

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