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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With