Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically save plots in ipython notebook

I would like to save to a different resolution all the plots generated in an ipython notebook. I know that I could add this line to each cell that shows a plot

plt.savefig('figure_1.pdf', dpi=300)

but that would require to add it manually when needed.

Is there any (reasonably) simple way to tell ipython to save each plot? Maybe by using a filename template like figure_X.pdf, where X is the cell's number?

Thanks

like image 799
mgalardini Avatar asked Sep 17 '26 14:09

mgalardini


1 Answers

Yes, just dynamically create a variable that is your desired output name.

outPDF = 'figure_{}.pdf'.format(cellNumber)
plt.savefig(outPDF, dpi=300)

So for example, if cellNumber = 5, outPDF = 'figure_5.pdf'

like image 62
Erica Avatar answered Sep 20 '26 04:09

Erica