Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATPLOTLIB: How do I have to provide font metrics files for rendering text by TeX?

I have a problem with Python (WinPython-64bit-3.6.5.0Qt5)/MATPLOTLIB (version 2.2.2) when rendering text with TeX (MikTeX 2.9) and application of the font "Times", which is in the list of 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 to reproduce the problem:

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 that python has no access to the TeX fonts, e.g. the rsfs10 which is located in my MikTeX installation folder C:\Program Files\MiKTeX 2.9\fonts\source\public\rsfs

If I do not specify the font name, it works with standard serif font (it look like computer modern serif).

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 (which I do not get in my full source code, and it is probably not related to the main problem):

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 already tried the solution for Windows described here: Matplotlib cannot find basic fonts but it didn't help to remove the warning. If I do not specify Times as font, the warning also is not raised.

like image 316
Stellarator Avatar asked Oct 19 '25 04:10

Stellarator


1 Answers

I had this exact same issue in matplotlib. While installing texlive is one way to do it, I preferred not to re-install another latex distribution in parallel, especially since I already spent some time getting everything set up and running with other external programs.

I did a little searching within the C:\Program Files\MiKTeX\miktex\bin\x64 folder and it looks like running:

miktex-maketfm.exe rsfs10

did the trick. It built rsfs10.tfm and put it within the C:\Users\XXX\AppData\Local\MiKTeX\fonts\tfm\public\rsfs folder.

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

like image 125
inverteddy Avatar answered Oct 22 '25 03:10

inverteddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!