Advertisement

Python matplotlib 解决 “not found because none of the following families were found: Times New Roman“

阅读量:

文章目录

  • 1. 问题

  • 2. 解决步骤

    • 2.1 下载 Times New Roman
    • 2.2 删除字体缓存
    • 2.3 下载字体
    • 2.4 下载fc-cache命令
  • 3. 使用字体


1. 问题

在 Linux Ubuntu/Centos 系统下,Python 画图/保存图片出现报错:

复制代码
    not found because none of the following families were found: Times New Roman"
    
    
    bash

原因是缺少 “Times New Roman” 字体,需要手动下载

2. 解决步骤

2.1 下载 Times New Roman

复制代码
    # ubuntu
    sudo apt-get update
    sudo apt-get install ttf-mscorefonts-installer
    
    # centos
    yum install ttf-mscorefonts-installer
    
    
    bash

如果是非图形化的系统,会出现一个界面,移动上下左右键,选择 “OK”安装。

2.2 删除字体缓存

命令行输入:Python
进入到 Python 终端界面

复制代码
    import matplotlib as mpl
    print(mpl.get_cachedir())
    
    
    bash

在这里插入图片描述
删除缓存:

复制代码
    rm -rf /root/.cache/matplotlib/*
    
    
    bash

2.3 下载字体

进入到 ~/.fonts 目录下(如果没有目录,那就创建 mkdir ~/.fonts

复制代码
    cd ~/.fonts
    
    
    bash

这里链接下载 SimHei 字体,或者执行下面命令:

复制代码
    wget http://129.204.205.246/downloads/SimHei.ttf
    
    
    bash

2.4 下载fc-cache命令

复制代码
    # centos 
    sudo yum install fontconfig -y
    
    # ubuntu
    sudo apt-get install fontconfig -y
    
    # mac
    brew install fontconfig
    
    
    bash

刷新字体缓存

复制代码
    fc-cache -fv
    
    
    bash

3. 使用字体

复制代码
    import matplotlib.pyplot as plt
    plt.rc('font', family="Times New Roman")
    
    
    python
    
    

全部评论 (0)

还没有任何评论哟~