findfont: Font family [‘Times New Roman‘] not found. Falling back to DejaVu Sans.

问题背景:

远程使用服务器绘图时,设置font_dict中字体格式为'Times New Roman',如下:

font_dict=dict(fontsize=16,
              color='black',
              family='Times New Roman',
            #   weight='light',
              style='italic',
              )

在绘图的过程中报出以下错误:

findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans.

错误原因:

服务器上缺少Times New Roman对应字体文件。

解决方案:

在服务器上安装 Times New Roman 字体文件。

step1:获取Times New Roman字体文件

        方法1(推荐):

        从win10系统上寻找,寻找路径是:‘C:\Windows\Fonts’,搜索“Times New Roman”,搜索结果如下。复制该文件到桌面,可以得到四个文件:times.tiff, timesbd.tiff, timesbi.tiff, timesi.tiff. 

在这里插入图片描述

        方法2:

        从网络上下载,下载网址是http://www.xiazaiziti.com/?s=times+new+roman.

step2:复制“time new roman”文件到服务器上

        把四个“time new roman”文件复制到服务器上  matplotlib库对应的 fonts/ttf 文件夹下,我的路径是“/home/wzg/anaconda3/envs/LSTM-C3D-GAN/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf”,该路径可以通过以下代码查看:

python
>>> import matplotlib
>>> print(matplotlib.matplotlib_fname())

step3:删除之前的字体缓存

        方法1:

        手工找到“home/wzg/.cache/matplotlib”目录并删除该目录,地址中的wzg是用户名,根据实际情况自行更改。如果找不到cache文件夹,是因为其为隐藏文件夹,可ctrl+h将其显示。

        方法2:

        在终端窗口输入以下指令寻找“home/wzg/.cache/matplotlib”目录,

python
>>> import matplotlib
>>> matplotlib.get_cachedir() 

找到隐藏的文件夹,如下图所示,

findfont: Font family [‘Times New Roman‘] not found. Falling back to DejaVu Sans.

 然后删除该文件夹。 

参考:Linux下的python修改画图的字体_刘西北的博客-CSDN博客_linux python 字体

使用matplotlib时缺失字体,findfont: Font family [‘Times New Roman‘] not found. Falling back to DejaVu Sans._ixobgenw的博客-CSDN博客

1238: UserWarning: findfont: Font family [‘sans-serif’] not found. Falling back to DejaVu Sans. – 灰信网(软件开发博客聚合)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年3月4日 下午1:07
下一篇 2023年3月4日 下午1:08

相关推荐