【问题标题】:matplotlib font not found未找到 matplotlib 字体
【发布时间】:2014-11-23 00:25:30
【问题描述】:

我正在尝试在我的 matplotlib 绘图中使用“Heuristica”字体,但它不会显示出来。

我在 rcParameter font.serif 的第一个位置定义了“Heuristica” --> 没有结果

我将 font.family 更改为“Heuristica”并收到消息

findfont: FontFamily not found

这让我开始思考,因为 Heuristica 已安装,我可以从其他软件毫无问题地访问它。所以我使用了 fontManager 并做了:

import pylab as pl
la = pl.matplotlib.font_manager.FontManager()
lu = pl.matplotlib.font_manager.FontProperties(family = 'Heuristica')
la.findfont(lu)

得到:

Out[7]: 'C:\\Windows\\Fonts\\Heuristica-Regular.otf'

显然可以找到 Heuristica。 我查找了可用的 ttf 字体 (How can i get list of font family(or Name of Font) in matplotlib),但 Heuristica 不在此列表中。

如果有任何帮助,我会很高兴。

【问题讨论】:

  • 你用的是什么版本的mpl?
  • 我使用的是 1.3.1 版
  • 尝试更新到 1.4.0。如果这不起作用,请在 github 上提出问题
  • 在 1.4.0 下问题依然存在。你确定这真的是 Github 的问题,而不是我电脑前的人的问题吗?关于 github 问题:我可以链接到这个问题还是复制粘贴?
  • 请提供足够的信息是我们不必链接回 SO 的问题。这里的问题不稳定(由于编辑和删除)。请提供足够的详细信息,以便使用 Windows 机器的人可以重现它(包括您是否更改了 rcparam 文件和 full 回溯)。

标签: python matplotlib fonts


【解决方案1】:

嗯,mdboom 在github 解决了问题,功劳归他所有:

当您向系统添加新字体时,您需要删除 fontList.cache 文件以便 matplotlib 找到它们。

它在您的示例中的第 4/5 行工作的原因是因为您正在从头开始创建一个 FontManager(它进入文件系统并搜索所有字体)。在内部,当 matplotlib 稍后进行自己的字体查找时,它使用的是从 fontList.cache 文件中的磁盘缓存加载的 FontManager。

从长远来看,我们计划改用操作系统的字体查找机制来解决这个问题(参见 MEP14),但与此同时,您需要在每次需要时删除 fontList.cache 文件matplotlib 来发现新字体。

文件 fontList.cache 位于您的用户文件夹 --> .matplotlib/fontList.cache,对于 Windows 通常为 C:\Users\yourUsername\.matplotlib\fontList.cache

【讨论】:

  • ~/.cache/matplotlib 在 Ubuntu 上。
  • 但是还是不行,字体管理器找不到合适的字体。
  • 我只在windows上试过这个解决方案,不能在Linux上测试。但也许您想在 github 问题中添加一些内容,这可能是获得错误修复的最佳方式:github.com/matplotlib/matplotlib/issues/3590
  • 我还不得不删除fontList.json
  • 强制matplotlib重建缓存可能是import matplotlib.font_manager as font_manager; font_manager._rebuild()最跨平台的解决方案
猜你喜欢
  • 2017-06-25
  • 2014-12-10
  • 2021-06-15
  • 2019-02-18
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多