Monday, July 19, 2010

Type 1 Fonts and matplotlib figures

Often times when you get a paper accepted for publication you're asked to submit the whole pdf using only Type 1 fonts and embed them in the document. I used matplotlib for all my figures and by default it used Type 3 fonts. In order to switch to Type 1 I had to throw in these lines:

matplotlib.rcParams['ps.useafm'] = True
matplotlib.rcParams['pdf.use14corefonts'] = True
matplotlib.rcParams['text.usetex'] = True


Finding those 3 lines somehow wasn't that trivial - I would've expected this to come up more often. Maybe my google-foo is no good here.

Update: as someone helpfully pointed out, the matplotlibrc is a more elegant way to accomplish this feat. The lines above can be added to the file, so you don't have to edit every one of the scripts (or digging for these lines around your code or the intertubes ever again).

17 comments:

  1. Nice. This saved me a lot of time one day before the deadline to submit CRC.

    ReplyDelete
  2. Thanks so much -- it was exactly what I needed!

    ReplyDelete
  3. thanks a lot from a utoronto grad student!

    ReplyDelete
  4. Thank you!! Although I was using amsfonts I couldn't figure out why I still had Type 3 fonts in my document. Apparently matplotlib embeds Cmb10 fonts that are not Type 1 by default.

    ReplyDelete
  5. Thanks for this, did the job beautifully!

    ReplyDelete
  6. Thanks! It is very helpful!

    ReplyDelete
  7. Black magic to me, but great anyway, thanks a lot!

    ReplyDelete
  8. You can set that in the "matplotlibrc" file. Look at http://matplotlib.org/users/customizing.html

    But for png figs, dvipng must be install !!!

    Thanks!!!

    ReplyDelete
  9. Thanks for the update. matplotlibrc is probably a better way to do this.

    ReplyDelete
  10. Thank you, I found this just in time :) Works like a charm!

    ReplyDelete
  11. First of all ...thanks! I have been puzzled with this the last couple of days!

    Although I have been using the rcParams['ps.fonttype'] = 42, which worked perfectly with other plots, in a specific case I couldn't make it. When I used the above lines it did saved the eps file with Type 1 fonts finally!

    After some small tests I think that (at least in my case!) only the last parameter (rcParams['text.usetex'] = True) was enough to use.

    Certainly the dvipng has to be installed or else you do not see anything at the matplotlib window!

    Thanks again!

    ReplyDelete
  12. Fantastic! Worked like a charm - and just a few hours before my submissioni deadline ;-) Thanks so much!!

    ReplyDelete
  13. Thanks! This saved me a lot of time!

    ReplyDelete
  14. Thank you very much! I wasted a whole day trying to figure this out!

    ReplyDelete