Comment centrer des figures matplotlib dans un Jupyter notebook ?

Published: 12 avril 2020

DMCA.com Protection Status

Exemple de comment centrer des figures matplotlib dans un Jupyter notebook ?

Afficher une image matplotlib dans une jupyter notebook:

Créons une simple plot avec matplotlib:

import matplotlib.pyplot as plt

plt.scatter([1,2,3,4,5,6,7,8],[4,1,3,6,1,3,5,2])

plt.title('Nuage de points avec Matplotlib')
plt.xlabel('x')
plt.ylabel('y')

Si on affiche cette figure dans un jupyter notebook avec

plt.show()

la figure sera alignée par défaut sur la gauche (voir image ci-dessous):

Comment centrer des figures matplotlib dans un Jupyter notebook ?
Comment centrer des figures matplotlib dans un Jupyter notebook ?

Centrer l'image dans le jupyter notebook:

Pour centrer l'image il suffit d'ajouter les lignes suivantes:

from IPython.core.display import HTML
HTML("""
<style>
.output_png {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
</style>
""")

voir image ci-dessous:

Comment centrer des figures matplotlib dans un Jupyter notebook ?
Comment centrer des figures matplotlib dans un Jupyter notebook ?

Références

Liens Site
Center output (plots) in the notebook stackoverflow
Image

of