1

I am using Windows 10 with Anaconda and Spyder 4. When using matplotlib, I would like to use the font Proxima Nova and render with LaTeX.

If in my matplotlibrc file I specify

font.family : Proxima Nova

then the figure renders with the font Proxima Nova. This means that the font is installed on my system (as it is) and matplotlib can use it. However, if in the matplotlibrc file I also specify

text.usetex: True

then, even though I have specified Proxima Nova as the font, the figure renders in the default LaTeX font, which I guess is Computer Modern.

I have tried

matplotlib.font_manager._rebuild()

In the source code file and also have tried specifying the fonts in the source code file and not in the matplotlibrc file. However I always get the same result. I have also followed all the advice on this help page, including making sure that latex, dvipng and ghostscript are all the PATH variable. However nothing seems to work.

I would like to note that I can use Proxima Nova separately when compiling Latex documents, so that should not be an issue either.

How can I get matplotlib to be able to use a non-default font and render with LateX at the same time?

jopeto
  • 31
  • 3

2 Answers2

1

After some further investigation, I was able to get to use Proxima Nova with Latex, although there are still some outstanding issues.

The main issue is that if the font Proxima Nova is used with Latex, one needs to use Lualatex and not plain Latex. Here is the Matplotlib instruction on using matplotlib with Lualatex.

The key to getting things to work was this post.

At the very beginning of my .py file, I have the following code:

import matplotlib as mpl

mpl.use("pgf")

mpl.rcParams.update({
    'font.family': 'sans-serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
    'pgf.texsystem': 'lualatex',
    'pgf.preamble': r'\usepackage{fontspec} \setmainfont{Proxima Nova}',
})

The code above should be placed at the very top of the code, above any other imports.

The problem, however is that this solution works only after performing the following steps:

  1. Delete the .matplotlib/tex.cache folder and restart spyder
  2. Replace 'font.family': 'sans-serif' and \setmainfont{Proxima Nova} with 'font.family': 'serif' and \setmainfont{Times New Roman} respectively. Run python once.
  3. Revert back to 'font.family': 'sans-serif' and \setmainfont{Proxima Nova} and run python again.
  4. The output with the correct font is produced.

Unless the above 4 steps are performed, the output is compiled with the default DejaVu Sans font and not with Proxima Nova. I am not sure why...

jopeto
  • 31
  • 3
0

After getting help on the matplotlib github forum, I was pointed to the following solution:

mpl.rcParams.update({
'font.family': 'sans-serif',
'text.usetex': True,
'pgf.rcfonts': False,
'pgf.texsystem': 'lualatex',
'pgf.preamble': r'\usepackage{fontspec} \setsansfont{Proxima Nova}',
})

In other words you need to use setsansfont in stead of setmainfont. You can see the matplotlib forum page here.

jopeto
  • 31
  • 3