Save a digit with multiple extensions?

In matplotlib, is there a (especially smart) way to save a shape with multiple extensions?

Use case. I usually want .png to quickly learn, upload to the Internet, etc. But for publication quality metrics, I need .pdf or .eps files. Often I need all 3.

This is not difficult:

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

but this is due to a lot of rewrite code (since I usually only have savefig(figname+'.png') everywhere), and this seems like an easy case for a convenient wrapper function.

+4
source share
2 answers

If you always do

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

then you could reassign pl.savefig with confidence in one place and 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 it will take a lot of effort.

+3
source

You can specify the file type, as shown in the documentation for savefig , using format . But as far as I know, you can specify only one format.

Not particularly smart, in order to minimize rewriting code, you have to write save_fig function. Then it can automatically process the corresponding file names and quality.

 def save_fig(fig_name, quality="quick-look"): if "quick-look" in quality: # save fig with .png etc if "pub-qual" in quality: # save fig with increased dpi etc etc. 

This is probably not the answer you are looking for, since you will need to rewrite each savefig file, but I feel that this can help reduce the amount of re-code in general.

+3
source

Source: https://habr.com/ru/post/1487916/


All Articles