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())大致查到该字体文件路径)
    在这里插入图片描述

方法二:网上下载

  • 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显示)。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值