Matplotlib et Notebook

Par défaut, les graphiques s'affichent comme avec un programme Python « normal ». Par exemple, le programme suivant ouvre une nouvelle fenêtre :

In [1]:
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 1000)
y = np.sin(x)
plt.plot(x, y)
plt.show()

On peut changer ce comportement et demander à afficher les graphiques directement dans le notebook (moins interactif, mais souvent plus pratique) comme ceci :

In [2]:
%matplotlib inline
In [3]:
plt.plot(x, y)
Out[3]:
[<matplotlib.lines.Line2D at 0x7f6679987b10>]

On peut revenir au comportement par défaut avec %matplotlib sans argument :

In [4]:
%matplotlib
Using matplotlib backend: TkAgg
In [5]:
plt.plot(x, y)
Out[5]:
[<matplotlib.lines.Line2D at 0x7f66791212d0>]