2

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.

bjarkemoensted
  • 2,557
  • 3
  • 24
  • 37

1 Answers1

5

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712