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 créer un tableau avec Matplotlib



Vous pouvez utiliser l’une des deux méthodes suivantes pour créer des tables en Python à l’aide de Matplotlib :

Méthode 1 : Créer une table à partir du DataFrame pandas

#create pandas DataFrame
df = pd.DataFrame(np.random.randn(20, 2), columns=['First', 'Second'])

#create table
table = ax.table(cellText=df.values, colLabels=df.columns, loc='center')

Méthode 2 : créer un tableau à partir de valeurs personnalisées

#create values for table
table_data=[
    ["Player 1", 30],
    ["Player 2", 20],
    ["Player 3", 33],
    ["Player 4", 25],
    ["Player 5", 12]
]

#create table
table = ax.table(cellText=table_data, loc='center')

Ce didacticiel fournit des exemples d’utilisation pratique de ces méthodes.

Exemple 1 : Créer une table à partir du DataFrame pandas

Le code suivant montre comment créer une table dans Matplotlib contenant les valeurs d’un DataFrame pandas :

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#make this example reproducible
np.random.seed(0)

#define figure and axes
fig, ax = plt.subplots()

#hide the axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')

#create data
df = pd.DataFrame(np.random.randn(20, 2), columns=['First', 'Second'])

#create table
table = ax.table(cellText=df.values, colLabels=df.columns, loc='center')

#display table
fig.tight_layout()
plt.show()

Table Matplotlib

Exemple 2 : Créer une table à partir de valeurs personnalisées

Le code suivant montre comment créer une table dans Matplotlib contenant des valeurs personnalisées :

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 

#define figure and axes
fig, ax = plt.subplots()

#create values for table
table_data=[
    ["Player 1", 30],
    ["Player 2", 20],
    ["Player 3", 33],
    ["Player 4", 25],
    ["Player 5", 12]
]

#create table
table = ax.table(cellText=table_data, loc='center')

#modify table
table.set_fontsize(14)
table.scale(1,4)
ax.axis('off')

#display table
plt.show()

table matplotlib avec des valeurs personnalisées

Notez que table.scale(width, length) modifie la largeur et la longueur du tableau. Par exemple, nous pourrions allonger encore le tableau en modifiant la longueur :

table.scale(1,10)

Tableau dans matplotlib

Ressources additionnelles

Comment ajouter du texte aux tracés Matplotlib
Comment définir le rapport hauteur/largeur dans Matplotlib
Comment changer la taille de la police de la légende dans Matplotlib

Ajouter un commentaire

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