Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython nbconvert and latex: use .eps instead of .png for plots

Tags:

ipython

I have an iPython notebook that contains an inline plot (i.e. it contains the command plot(x,y)). When I issue the command ipython nbconvert --to latex --post PDF --SphinxTransformer.author='Myself' MyNotebook.ipynb the resulting .PDF file contains the figure, but it has been exported to .PNG, so it doesn't look very good (pixelated). How can I tell nbconvert to export all plots/figures to .EPS instead?

Thank you

like image 981
luffe Avatar asked Oct 29 '13 13:10

luffe


1 Answers

First of all the notebook is not responsible for creating plots, but matplotlib and this allows to render your plots as eps, pdf, svg, etc. in great quality to be included in publications.

I agree that the default inline plot format, i.e. png is not optimal to be used for publications due to several reasons. As given in the github issue you posted in your answer, the inline backend can be configured to use svg instead of png by calling

%config InlineBackend.figure_format = 'svg'

in a code cell. With this, the newly created plots will be vectorial, (as Matt said, already rendered pngs will not be converted!). These svgs are embedded in the notebook (svgs can be rendered by modern browsers) and are subsequently converted to pdfs by nbconvert. These pdfs fulfil the requirements of publication ready plots. However, be aware that the svgs can be really huge (compared with pngs) and may slow down the notebook handling significantly.

Your initial question was about eps graphics. As said above, matplotlib can render eps, hence, you can always do something like savefig('plot1.eps') to create the desired figures. That's actually the way I create my publication figures (png in notebook, eps in paper). Let's assume we would get IPython to generate eps files (embedded but not renderable in the browser). The tex file generated by nbconvert is designed to work with pdflatex. If fed with eps files pdflatex would convert these to pdf to be included in the final document. So basically it does the same as nbconvert currently does with the svgs. Thus, there is no benefit.

Finally, I want to point out that, even though the tex files generated by nbconvert look great (especially the ones created using the master branch), IMHO there is no way to use these files without touching (e.g. adding captions, scaling images, ...). Therefore, you could always include the eps files at this step into your papers.

like image 91
Jakob Avatar answered Oct 15 '22 22:10

Jakob