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

China☆狼群 提交于 2020-01-02 11:24:41

问题


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 https://matplotlib.org/users/customizing.html)

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.


回答1:


With the discussion done here https://tex.stackexchange.com/questions/442179/why-are-tfm-files-missing-in-the-latex-rsfs-package I have found a solution:

On my computer, the MikTex installation was quite new, and the font has never been used before, therefore the required tfm files had not been created.

However, the matplotlib Python package (dviread.py) tried to identify the tfm files before usage in order to create a fontfile cache.

I have installed a complete TeXLive installation on another computer. There the tfm files were already created during installation and I just copied the files to the corresponding location on my computer.



来源:https://stackoverflow.com/questions/50875637/matplotlib-how-do-i-have-to-provide-font-metrics-files-for-rendering-text-by-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!