How to export figures to files from IPython Notebook
110,778
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.
Related videos on Youtube
Author by
clstaudt
Updated on July 09, 2022Comments
-
clstaudt almost 2 years
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.
-
Jonas over 5 yearsI use this little hack
jupyter nbconvert --to markdown *.ipynb
. It generates a directory per notebook to store images
-
-
clstaudt over 11 yearsThanks. Could you tell me quickly how to change the default notebook folder? I tried setting $IPYTHONDIR, but this doesn't work.
-
root over 11 yearsjust 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.
-
clstaudt over 11 yearsOkay, but relocating my notebooks is what I want.
-
Paul H over 11 years@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
-
Bruno Feroleto over 11 years@root: It looks like the key point is to have
savefig()
in the same cell as the plotting commands. In fact, aplot()
in one cell and asavefig()
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. -
Zach Dwiel about 10 yearsthanks @EOL, having the same issue on IPython 0.13, Ubuntu 12.04 and took a long time to find this critical note
-
Bruno Feroleto about 10 years@ZachDwiel: Thanks for the feedback. I edited the answer so as to make this important "detail" more visible.
-
braunmagrin about 9 years@EOL @ZachDwiel Does any of you know why the
savefig()
must be in the same cell as the plotting commands? -
Bruno Feroleto about 9 years@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.
-
braunmagrin about 9 years@EOL It seems that it closes the figures to prevent memory leakage. I've posted a question about this yesterday and other users posted good insights there about this.
-
Bruno Feroleto about 9 yearsThanks 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).