The only blog not featuring an ipod.

How to solve the bounding box problem with matplotlib generated eps files

The situation is the following: you generate an eps file using matplotlib and then import it into a latex document which you compile to pdf. Some viewers have no problems with it, other's refuse to open it (old versions of chrome's viewer on macosx, for example). Furthermore, some  latex compilers will crop / truncate the image when converting it to pdf as an intermediate step.


I have come across this scenario when doing a submission using manuscriptcentral that included some plots generated in matplotlib, which were quite large in size. Smaller plots seem to have no problem. The compiler on the site generated the proof pdf with the large images truncated on the left. Rotating the image in latex didn't solve the problem.

Apparently, the way compilers and viewers handle postscript bounding boxes that have corners with negative coordinates is inconsistent and has been so for over 10 years.  One way to go around it, taken from here, is the following:

suppose the offending file is called  plot.eps

  gs -o testaa.pdf -sDEVICE=pdfwrite -dEPSCrop plot.eps 
  gs -o plot.eps -sDEVICE=epswrite testaa.pdf 

gs is the command for ghostscript, a decades old compiler viewer for postscript and related files. It is available both for debian and windows, at least. 












Alguien me habló todos los días de mi vida al oido, despacio, lentamente. Me dijo: ¡vive, vive, vive! Era la muerte. (JS)