【问题标题】:Matplotlib reports font family 'serif' not foundMatplotlib 报告找不到字体系列“衬线”
【发布时间】: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]))

我尝试过的事情:

  1. 删除fontList.cachefontList.jsonfontList.py3.cache

  2. 取消注释matplotlibrc 文件中与字体系列相关的部分

  3. 使用pip uninstall matplotlibpip 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


【解决方案1】:

我遇到了同样的问题。对我来说,将我的 matplotlib 版本从回更改为 1.5.3 解决了这个问题。你需要先pip uninstall matplotlib 然后pip install matplotlib==1.5.3

更多上下文,我在带有 Amazon EC2 的 zeppelin 笔记本上使用 ggplot。不知何故,安装的 matplotlib 版本是2.2.3

更改版本后,错误消失了

【讨论】:

    猜你喜欢
    • 2021-12-18
    • 2014-01-09
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    相关资源
    最近更新 更多