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.

Pandas : Comment annoter des barres dans un tracé à barres



Vous pouvez utiliser les méthodes suivantes pour annoter les barres dans un tracé à barres pandas :

Méthode 1 : Annoter les barres dans un tracé à barres simple

ax = df.plot.bar()

ax.bar_label(ax.containers[0])

Méthode 2 : Annoter les barres dans un tracé à barres groupées

ax = df.plot.bar()

for container in ax.containers:
    ax.bar_label(container)

Les exemples suivants montrent comment utiliser chaque méthode dans la pratique.

Exemple 1 : Annoter des barres dans un tracé à barres simple

Le code suivant montre comment annoter des barres dans un simple tracé à barres :

import pandas as pd

#create DataFrame
df = pd.DataFrame({'product': ['A', 'B', 'C', 'D', 'E'],
                   'sales': [4, 7, 8, 15, 12]})

#view DataFrame
print(df)

  product  sales
0       A      4
1       B      7
2       C      8
3       D     15
4       E     12

#create bar plot to visualize sales by product
ax = df.plot.bar(x='product', y='sales', legend=False)

#annotate bars
ax.bar_label(ax.containers[0])

les pandas annotent le tracé à barres

Notez que la valeur réelle des ventes est affichée en haut de chaque barre.

Exemple 2 : Annoter des barres dans un tracé à barres groupées

Le code suivant montre comment annoter les barres dans un tracé à barres groupées :

#create DataFrame
df = pd.DataFrame({'productA': [14, 10],
                   'productB': [17, 19]},
                    index=['store 1', 'store 2'])

#view DataFrame
print(df)

         productA  productB
store 1        14        17
store 2        10        19

#create grouped bar plot
ax = df.plot.bar()

#annotate bars in bar plot
for container in ax.containers:
    ax.bar_label(container)

les pandas annotent les barres dans un tracé à barres groupées

Notez que des annotations ont été ajoutées à chaque barre individuelle du tracé.

Ressources additionnelles

Les didacticiels suivants expliquent comment créer d’autres visualisations courantes dans les pandas :

Comment créer un boxplot à partir de Pandas DataFrame
Comment créer un diagramme circulaire à partir de Pandas DataFrame
Comment créer un histogramme à partir de Pandas DataFrame

Ajouter un commentaire

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