Python – MATPLOTLIB: How do I provide a font specification file to render text over TeX?

MATPLOTLIB: How do I provide a font specification file to render text over TeX?… here is a solution to the problem.

MATPLOTLIB: How do I provide a font specification file to render text over TeX?

There

is a problem with Python (WinPython-64bit-3.6.5.0Qt5)/MATPLOTLIB (version 2.2.2) when rendering text and the font “Times” in the application list using TeX (MikTeX 2.9) standard fonts (see Customizing matplotlib )

In the minimal example below, I get the following error message:

  File "C:\WinPython-64bit-3.6.5.0Qt5\python-3.6.5.amd64\lib\site-packages\matplotlib\dviread.py", line 471, in _fnt_def_real
    raise error_class("missing font metrics file: %s" % fontname)
FileNotFoundError: missing font metrics file: rsfs10

Here is an example of reproducing the issue:

import numpy as np
import matplotlib.pyplot as plt

# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2

from matplotlib import rc
rc('font',**{'family':'serif','serif':['Times']})
rc('text', usetex=True)

plt.plot(t, s)

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
          r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo')
plt.show()

It looks like python can’t access TeX fonts, e.g. rsfs10 located in my MikTeX install folder
C:\Program Files\MiKTeX 2.9\fonts\source\public\rsfs

If I don’t specify the font name, it uses a standard serif font (it looks like a modern serif for a computer).

from matplotlib import rc
rc("pdf", fonttype=3)
rc('font',**{'family':'serif'})
rc('text', usetex=True)

I

have to add the following: In the minimal example below, I first get a warning message (I didn’t get it in my full source code, it may not be related to the main issue):

C:\WinPython-64bit-3.6.5.0Qt5\python-3.6.5.amd64\lib\site-packages\matplotlib\font_manager.py:1328: UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans (prop.get_family(), self.defaultFamily[fontext]))

I’ve tried the Windows solution described here: Matplotlib Cannot find basic fonts, but it doesn’t help remove warnings. If I don’t specify Times as a font, it doesn’t raise a warning.

Solution

I’m having the exact same issue in matplotlib. While installing texlive is one way to do this,
I don’t want to reinstall another latex distro at the same time, especially since I’ve spent some time setting everything up and running it with other external programs.

I did some searching in the C:\Program Files\MiKTeX\miktex\bin\x64 folder and it looks like it’s running:

miktex-maketfm.exe rsfs10

It worked. It builds rsfs10.tfm and places it in the C:\Users\XXX\AppData\Local\MiKTeX\fonts\tfm\public\rsfs folder.

This solution works with Miktex 20.10. Once I ran this, matplotlib had no problem. Note that this post is a copy of my answer here

Related Problems and Solutions