【发布时间】:2019-02-18 14:49:10
【问题描述】:
当我尝试运行时,例如:
mpl.rcParams['font.family'] = 'serif'
plt.rcParams['figure.figsize'] = [15,7]
plt.plot(data['flow-time'], data['staticpressurerecovery'])
plt.xlabel('Time [s]')
plt.ylabel('Static Pressure Recovery [-]')
plt.title('McD13_4S3 Plenum: Performance Coefficient ')
plt.ylim((0.33, 0.4))
plt.grid()
plt.show()
在 Jupyter 笔记本中,我收到以下错误消息:
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\font_manager.py:1331: UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))
我尝试过的事情:
删除
fontList.cache、fontList.json和fontList.py3.cache取消注释
matplotlibrc文件中与字体系列相关的部分使用
pip uninstall matplotlib和pip install matplotlib卸载并重新安装matplotlib
没有解决这个问题。我现在获得不同字体的唯一可能方法是使用 LaTeX 作为后端,但这很慢而且没有必要。
有什么我可以尝试的想法吗?
编辑:我使用的是 Windows 10,所以我没有使用 apt-get。这似乎是解决这些问题的常见方法,但我做不到。似乎这些解决方案只是将 Microsoft 字体添加到 Linux 字体管理器中,因此它可能甚至不相关,因为我已经在 Microsoft 机器上。
最小工作示例:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = 'Computer Modern'
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.show()
在fontList.json 中,Computer Modern 被列为可用字体。
【问题讨论】:
-
这肯定是一个奇怪的错误,因为它当然应该工作(并且对我有用)。我假设您确实在两者之间重新启动了内核,对吧?所以在这种情况下,第一件事总是产生minimal reproducible example。在这种情况下,这将是 3 或 4 行长,并且将是您笔记本中唯一的内容。如果这重现了错误,您至少可以确定您在代码的其余部分中没有做任何奇怪的事情。
-
ImportanceOfBeingErnest 我已经用一个工作示例更新了这个问题。现在问题略有改变,但我仍然无法弄清楚为什么它不能正常工作。
-
我认为计算机现代是
mathtext.fontset的有效字体集,但至少我的 Windows 计算机没有安装计算机现代作为 ttf 字体。但是,mpl.rcParams['font.serif'] = 'Times New Roman'之类的东西应该可以在任何 Windows 计算机上运行。 -
我已将 'Computer Modern' ttf 添加到 matplotlib 的数据目录中,这些文件也显示在
fontsList.json文件中。
标签: python matplotlib fonts