Matplotlib 报告字体系列 'serif' 未找到。

8
当我尝试运行,例如:
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。那似乎是这些问题的常见解决方案,但我做不到。它似乎只是向Linux字体管理器添加Microsoft字体,因此对我来说可能并不相关,因为我已经在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 被列为可用字体。

1
这确实是一个奇怪的错误,因为它当然应该工作(对我来说也是如此)。我假设你在中间重新启动了内核,对吗?所以在这种情况下,第一件事就是生成一个 [mcve]。在这种情况下,它将只有3或4行,并且将是笔记本中唯一的内容。如果这样可以重现错误,那么您至少可以确定在代码的其余部分中没有做出奇怪的事情。 - ImportanceOfBeingErnest
我已经更新了这个问题,并提供了一个工作示例。问题现在略有改变,但是我仍然无法弄清楚为什么它没有正常工作。 - James Wright
我认为Computer Modern是mathtext.fontset的有效字体集,但是我的Windows电脑上并没有安装Computer Modern作为TTF字体。类似于 mpl.rcParams ['font.serif'] ='Times New Roman'这样的设置在任何Windows计算机上都可以使用。 - ImportanceOfBeingErnest
我已经将“Computer Modern” ttf添加到matplotlib的数据目录中,这些文件也显示在“fontsList.json”文件中。 - James Wright
1个回答

0

我遇到了相同的问题。对我来说,将matplotlib版本从 2.2.3 改回 1.5.3 可以解决该问题。您需要先执行 pip uninstall matplotlib 然后再执行 pip install matplotlib==1.5.3

更多背景信息,我在Amazon EC2上使用zeppelin笔记本,并使用 ggplot 。不知何故,安装的matplotlib版本为 2.2.3

更改版本后,错误已经消失。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接