Ubuntu Linux,及Python matplot,安装Times New Roman等字体,让图标签可以用Times New Roman等字体

Ubuntu Linux,及Python matplot,安装Times New Roman等字体,让图标签可以用Times New Roman等字体

error1238: UserWarning: findfont: Font family [‘sans-serif’] not found. Falling back to DejaVu Sans.

1、查看现有的字体,没安装的话一定没有Times New Roman,黑体啥的

import matplotlib.pyplot as plt
#plt.rcParams['font.sans-serif']=['Times New Roman']
import matplotlib 
a = sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
previous_font = '   '
count = 0 
for font_name in a:
    if(font_name != previous_font):
        count = count + 1  
#         print(font_name, '\t\t\t\t', end='')
        if(count > 2): 
            print('{:35}'.format(font_name), end='') 
        if(count % 1 == 0):
            print()
        previous_font = font_name

2、自动安装字体库(当然也有手动的,我手动失败了,自动成功了)

主要参考了下面的两个链接
http://www.360doc.com/content/21/0325/10/39461177_968764875.shtml
https://www.freesion.com/article/1473488596/
下面简明介绍我的方法

用apt-get工具安装Times New Roman等英文字体,即安装ttf-mscorefonts-installer,但此字体包,不包括楷体,黑体等。可能黑体需要手动下载安装
1、安装必要的支持库软件,我没有报错,如果报错,参考上面链接

sudo apt-get install fontconfig
sudo apt install ttf-mscorefonts-installer

在这里插入图片描述
还是那个问题,我没有报错,TAB键移动光标到yes,回车就OK了,如果你报错了,请参考https://www.freesion.com/article/1473488596/
2、也可以用以下命令来测试:

fc-match Times
fc-match "Times New Roman"

说明成功了
3、刷新Ubuntu系统字体缓存,和matplotlib字体缓存. (Ubuntu系统字体缓存我没刷新就能用,matplotlib的字体缓存区要刷新, matplotlib才能用,重要)
在终端python 的环境下,输入如下指令,查看matplotlib的字体缓存路径:

https://blog.csdn.net/sinat_40875078/article/details/104326855参考这篇

import matplotlib
matplotlib.get_cachedir()

得到一个路径
4、然后使用rm -rf +路径删除缓存
在这里插入图片描述
5、安装黑体参考下面的链接(手动)

https://blog.csdn.net/sinat_40875078/article/details/104326855
6、方法或有出入,主要是手动下载和自动源文件的区别

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值