52

I use the IPython Notebook with the --pylab inline option, since I don't want plots to show up in a different window. Now I'd like to save the plots I see in the notebook to PDF or PNG files.

Some code examples use

import matplotlib as plt

plt.savefig("figure.png") # save as png

but this does not seem to work in inline mode.

Of course I could simply save the PNG that is generated out of the browser, but I'd like to do this with a line of Python. I am also interested in PDF export.

clstaudt
  • 21,436
  • 45
  • 156
  • 239
  • 5
    I use this little hack `jupyter nbconvert --to markdown *.ipynb`. It generates a directory per notebook to store images – Jonas Nov 22 '18 at 16:15

1 Answers1

56

try this (note that the files get saved to the default notebook folder):

plot(range(80))
xlabel('foo')
ylabel('bar')
legend(['myline'])
axis([0, 80, 0, 120])
savefig('sample.pdf')

if you want png just change it to 'sample.png'.

Note that the savefig() call should be in the same notebook cell as the plotting commands.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
root
  • 76,608
  • 25
  • 108
  • 120
  • Thanks. Could you tell me quickly how to change the default notebook folder? I tried setting $IPYTHONDIR, but this doesn't work. – clstaudt Nov 30 '12 at 10:32
  • 2
    just specify the folder in your path like: 'home/foo/bar/sample.pdf' if you want to save plots to some other folder. changing the default dir will also relocate your notebook files. – root Nov 30 '12 at 10:45
  • Okay, but relocating my notebooks is what I want. – clstaudt Nov 30 '12 at 11:35
  • 4
    @cls first copy the `*.ipynb` files to the directory where you want them. then start the notebook server with `$ ipython notebook --pylab=inline --notebook-dir=/path/to/my/notebooks` – Paul H Dec 02 '12 at 23:31
  • 17
    @root: It looks like the key point is to have `savefig()` in the same cell as the plotting commands. In fact, a `plot()` in one cell and a `savefig()` in the next cell does not save the file (IPython 0.13 for Mac OS X via MacPorts). It would be nice if this were stressed in your response. – Eric O. Lebigot Dec 27 '12 at 03:54
  • thanks @EOL, having the same issue on IPython 0.13, Ubuntu 12.04 and took a long time to find this critical note – Zach Dwiel Apr 08 '14 at 19:52
  • @ZachDwiel: Thanks for the feedback. I edited the answer so as to make this important "detail" more visible. – Eric O. Lebigot Apr 09 '14 at 03:33
  • 1
    @EOL @ZachDwiel Does any of you know why the `savefig()` must be in the same cell as the plotting commands? – braunmagrin Mar 04 '15 at 16:07
  • 1
    @braunmagrin Good question. I was surprised by this, at the beginning. I can guess that when you open a notebook, there is no current figure, since the notebook generally contains many figures. By extension, when quitting a cell, there is no current figure, so savefig() has no target. Again, I'm just guessing… I am not sure why the last figure plotted could not be the current figure, by convention. – Eric O. Lebigot Mar 05 '15 at 02:40
  • 1
    @EOL It seems that it closes the figures to prevent memory leakage. I've posted a [question](http://stackoverflow.com/questions/28859965/why-does-savefig-and-plot-commands-have-to-be-in-the-same-cell-in-an-ipython-not) about this yesterday and other users posted good insights there about this. – braunmagrin Mar 05 '15 at 11:15
  • 1
    Thanks for the pointer. It looks like the inline figures are converted to PNG or SVG, and the original Matplotlib figure is lost (so as to free memory). In this case, there is indeed no way of creating, say, a PDF. Now, I am not sure why the notebook would not keep the last figure in memory… (`gcf().savefig()` saves a blank figure). – Eric O. Lebigot Mar 06 '15 at 08:54
  • Can this answer be edited to give appropriate imports? It looks like it needs several `from matplotlib.pyplot import ` lines. – Dave Aug 30 '23 at 09:20