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
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、方法或有出入,主要是手动下载和自动源文件的区别