Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a figure with multiple extensions?

Tags:

matplotlib

In matplotlib, is there any (especially clever) way to save a figure with multiple extensions?

Use case: I generally want .png's for quick-look examination, uploading to the web, etc. But for publication-quality figures, I want .pdf or .eps files. Often, I want all 3.

It's not hard to do:

for suffix in 'png eps pdf'.split():
    pl.savefig(figname+"."+suffix)

but it does involve a lot of rewriting code (since I generally just have savefig(figname+'.png') everywhere right now), and this seems like an easy case for a convenient wrapper function.

like image 591
keflavich Avatar asked Jun 24 '13 15:06

keflavich


1 Answers

If you always do

from matplotlib import pyplot as pl
...
pl.savefig

then you could concievably reassign pl.savefig in one place and it would affect all places.

from matplotlib import pyplot as pl
def save_figs(fn,types=('.pdf',)):
    fig = pl.gcf()
    for t in types:
        fig.savefig(fn+t)
pl.savefig = save_figs

If you usually do

fig=pl.figure()
fig.savefig(...)

then that will require more effort.

like image 118
esmit Avatar answered Jan 02 '23 22:01

esmit