Alignement du texte dans Matplotlib

Beginner

This tutorial is from open-source community. Access the source code

Introduction

Matplotlib est une bibliothèque de tracé pour le langage de programmation Python et son extension NumPy pour les mathématiques numériques. Ce tutoriel se concentrera sur l'alignement du texte dans Matplotlib.

Conseils sur la machine virtuelle

Une fois le démarrage de la machine virtuelle terminé, cliquez dans le coin supérieur gauche pour basculer vers l'onglet Notebook pour accéder à Jupyter Notebook pour la pratique.

Parfois, vous devrez peut-être attendre quelques secondes pour que Jupyter Notebook ait fini de charger. La validation des opérations ne peut pas être automatisée en raison des limitations de Jupyter Notebook.

Si vous rencontrez des problèmes pendant l'apprentissage, n'hésitez pas à demander à Labby. Donnez des commentaires après la session, et nous résoudrons rapidement le problème pour vous.

Création d'un rectangle

Nous allons commencer par créer un rectangle dans le tracé à l'aide de la fonction Rectangle() du module matplotlib.patches. Après avoir créé le rectangle, nous allons définir ses limites horizontales et verticales à l'aide des fonctions set_xlim() et set_ylim(). Enfin, nous ajouterons le rectangle au tracé.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, ax = plt.subplots()

## Build a rectangle in axes coords
left, width =.25,.5
bottom, height =.25,.5
right = left + width
top = bottom + height
p = Rectangle((left, bottom), width, height, fill=False)
ax.add_patch(p)

## Set the horizontal and vertical limits
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()

Ajout de texte au rectangle

Dans cette étape, nous allons ajouter du texte au rectangle à l'aide de la fonction text(). L'alignement horizontal et vertical du texte sera défini respectivement à l'aide des paramètres horizontalalignment et verticalalignment.

## Add text to the rectangle
ax.text(left, bottom, 'left top',
        horizontalalignment='left',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, bottom, 'left bottom',
        horizontalalignment='left',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right bottom',
        horizontalalignment='right',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right top',
        horizontalalignment='right',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(right, bottom, 'center top',
        horizontalalignment='center',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, 0.5 * (bottom + top), 'right center',
        horizontalalignment='right',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, 0.5 * (bottom + top), 'left center',
        horizontalalignment='left',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(0.5 * (left + right), 0.5 * (bottom + top),'middle',
        horizontalalignment='center',
        verticalalignment='center',
        transform=ax.transAxes)

ax.text(right, 0.5 * (bottom + top), 'centered',
        horizontalalignment='center',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, top, 'rotated\nwith newlines',
        horizontalalignment='center',
        verticalalignment='center',
        rotation=45,
        transform=ax.transAxes)

plt.show()

Personnalisation du tracé

Dans cette étape, nous allons personnaliser le tracé en ajoutant des étiquettes d'axe et en supprimant les lignes d'axe.

## Customize the plot
ax.set_axis_off()
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_title('Text Alignment in Matplotlib')
plt.show()

Sommaire

Dans ce tutoriel, nous avons appris à aligner le texte dans Matplotlib. Nous avons utilisé la fonction text() pour ajouter du texte à un rectangle et défini l'alignement horizontal et vertical respectivement à l'aide des paramètres horizontalalignment et verticalalignment. Nous avons également personnalisé le tracé en ajoutant des étiquettes d'axe et en supprimant les lignes d'axe.