mgalardini
mgalardini

Reputation: 1467

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

Upvotes: 3

Views: 2090

Answers (1)

Erica
Erica

Reputation: 2477

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'

Upvotes: 3

Related Questions