findfont: Font family ‘Times New Roman‘ not found.

问题

Linux 使用 matplotlib.pyplot 画图时为了使字体和英文论文中的 Times of Roman 一致,通常会用到如下文本格式

font1 = {'family': 'Times New Roman',  # x and y labels
         'weight': 'normal',
         'size': 16
         }

但在实际使用时会出现如下报警信息:

findfont: Font family 'Times New Roman' not found.

也就是系统找不到 Times New Roman 字体。所以解决的方式也就很直接:往 matplotlib 包中加 Times New Roman 字体。

主要涉及两个步骤:(1)加字体;(2)删缓存。

(1)加字体

方法一:windows系统中找

  • 在系统字体路径C:\Windows\Fonts下搜索Times New Roman(搜索结果如下)
  • 将搜到的字体文件拷贝到 linux 系统中 matplotlib 包的字体文件夹下:/home/amax/anaconda3/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf (可通过 import matplotlib, print(matplotlib.matplotlib_fname())大致查到该字体文件路径)
    findfont: Font family ‘Times New Roman‘ not found._第1张图片

方法二:网上下载

  • 在 xiazaiziti.com 上下载 Times New Roman 字体文件
  • 将下载的字体文件拷贝到 linux 系统中 matplotlib 包的字体文件夹下:/home/amax/anaconda3/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf (可通过 import matplotlib, print(matplotlib.matplotlib_fname())大致查到该字体文件路径)

(2)删缓存

删除或备份(修改文件夹名)/home/amax/.cache/matplotlib目录(可通过查找到的字体文件夹路径的相对路径方式找;也可以通过代码找:import matplotlib, matplotlib.get_cachedir())。如果找不到.cache文件夹,可能是被隐藏了(可通过 ctrl+h显示)。

你可能感兴趣的:(linux,python,ubuntu)