简体   繁体   中英

matplotlib fonts not matching LaTeX fonts

I'm currently starting to write up my PhD thesis and am now in the middle of setting up everything. One of the objectives is to use the same font in schemes and figures as in the text. However, when selecting bold text in matplotlib the fonts are not matching the latex font.

At the moment I am using these rc_params:

new_rc_params = {'text.usetex': True,
         'svg.fonttype': 'none',
         'text.latex.preamble': r'\usepackage{libertine}',
         'font.size': 10,
         'font.family': 'Linux Libertine',
         'mathtext.fontset': 'custom',
         'mathtext.rm': 'libertine',
         'mathtext.it': 'libertine:italic',
         'mathtext.bf': 'libertine:bold'
         }

mpl.rcParams.update(new_rc_params)

This will lead to the right font in normal text as far as I can tell.
Matplotlib:
matplotlib1

LaTeX comparison:
乳胶比较1

If I now, however, select bold text in matplotlib via:

$\mathbf{TS_{I,II}}$

I get the following output:
matplotlib2

For comparison the bold and normal pure LaTeX output:
乳胶比较2

Now, I have read that for better math support in LaTeX I should add \\usepackage[libertine]{nextxmath} to text.latex.preamble in rc_params . If I do this, matplotlib generates the following output:

matlplotlib3

Which still is not the same font as I get from my LaTeX document. Does anyone have any ideas what I could additionally change to make the fonts identical?

(Can't post inline images...)

I figured it out, with the help of the comment from @Patol75

So it is indeed rather a "pythonic" problem. For some reason if you try to use:

$\textbf{...}$

in a matplotlib label, it gets rendered like the following:

matplotlib4

For some reason that I did not look into further, LaTeX commands starting with \\b... spit out an error . Which prevented the easy use of \\bm . However, if one uses an r' in front of the command everything works fine. Therefore, one can use:

r'\textbf{TS$_{\textbf{I,II}}$}'

This produces the correct/expected output:
matplotlib5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM