bjarkemoensted
bjarkemoensted

Reputation: 2766

How do I display a previously generated plot in a Jupyter notebook?

I'm running a Jupyter notebook to make some plots:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

x = np.arange(0, 7, 0.01)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x))

When I execute this, the plot is rendered and displayed as output. However, I'd like to view it later in the notebook, but plt.show() does nothing, and fig.show() warns me that matplotlib is using a non-GUI backend. However, I've tried using all the backends listed in this answer without luck, and I'm confused as to how the plot can render in the first place if I'm using a non-GUI backend. fig.savefig(filename) works fine, however.

Upvotes: 3

Views: 1572

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339280

Using the %matplotlib inline backend, figures in jupyter are shown as png image. Hence you do not have an interactive backend available, which would allow to use fig.show().
On the other hand, the inline backend will ensure that a figure is automatically shown once it it referenced within a cell. Hence, simply stating the figure handle fig inside a cell will append the figure to the cell output.

enter image description here

Upvotes: 6

Related Questions