0

I know there are already several questions about this (e.g. Sans-serif math with latex in matplotlib) , but I keep having problems with setting the latex font from within matplotlib. I am using

font = {'family' : 'serif',
    'serif': 'helvet',
    'weight' : 'bold',
    'size'   : 20}

matplotlib.rc('font', **font)
matplotlib.rc('text', usetex=True)

This looks right on the first glance, but when one looks more closely it seems that the width of the letters in the figure does not match the width of the letters in a manuscript compiled with latex (see screenshot).

enter image description here

Note that this question is about the text in the figures, not about the math-font in them (which works perfectly), and that it is also not simply an issue about the font size. So I have two questions:

  1. Does the serif-setting in the font do anything for my parameters (I have also used Computer Modern Roman, but couldn't tell the difference).

  2. Is there a way to get this font exactly right?

I know this might look like I'm a stickler, but I use matplotlib for my work where I do need to match these fonts precisely. If this is impossible, I'll need to use an external graphics (or latex/psfrag) software to insert the font after producing the figure - which is okay, but I'd be grateful to know what people have thought about this or handle these issues before I stop trying. Thanks!

mzzx
  • 1,964
  • 4
  • 16
  • 26
  • So you are sure to be using the same font in your latex document as well? I think latex may adjust letter spacing in paragraphs, so is there still a difference if you put the single word "Metapopulation" in latex and in your figure? If you want people to further look at this it might help to provide a [mcve] including the latex document. – ImportanceOfBeingErnest Jul 12 '18 at 10:50
  • What format are you saving your figure in? – Thomas Kühn Jul 12 '18 at 11:08
  • I'm saving the figure as pdf (using savefig) -- can this make a difference?? – mzzx Jul 12 '18 at 12:06
  • And no, I think the letter width is constant, but the spacing between words varies (I'm unsure about the letter spacing, but yes, there is a noticeable difference even when I put "Metapopulation" in at a single word). It is also definitely notable that the font is somehow just this tiny but more stretched in height. (Also, even the letter "p" is different, the lower horizontal line is somehow wider and bolder in the latex.) I think normally this is fine because often the font is not compared this meticulously, but apparently in this case people did notice. – mzzx Jul 12 '18 at 12:08
  • I'll post a verifiable example later but I also just wanted to check if I was doing something wrong that's obvious - if it looks more subtle it might not be worthwhile, and it might be better to just not do this in matplotlib (which is fine because arguably matplotlib's main purpose is not to match tex 100%) – mzzx Jul 12 '18 at 12:10

0 Answers0