How to center a matplotlib figure in a Jupyter notebook ?

Published: April 14, 2020

Tags: Jupyter Notebook;

DMCA.com Protection Status

Example of how to center a matplotlib figure in a Jupyter notebook:

Plot a matplotlib figure in a Jupyter notebook

Let's create a simple matplotlib figure:

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')

To show the figure in the Jupyter notebook just add:

plt.show()

by default will be align on the left:

How to center a matplotlib figure in a Jupyter notebook ?
How to center a matplotlib figure in a Jupyter notebook ?

Centrer l'image dans le jupyter notebook:

To then center the image, a solution is to add the following lines:

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

see images below

How to center a matplotlib figure in a Jupyter notebook ?
How to center a matplotlib figure in a Jupyter notebook ?

References

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

of