其中字体文件可以自行下载或者直接从windows目录下拷贝。 windows下步骤相同,唯一变化的是对应文件目录不同,根据自己情况进行查找即可。 详细步骤如下: (1)上传.ttf文件至matplotlib的font目录(/usr/lib64/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf)下; (2)上传.tff文件至系统font目录(/usr/share/fonts/)下; (3)安装font-config(指令:yum install font-config); (4)执行下列代码:
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
from matplotlib.font_manager import FontManager
import subprocess
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)
# print '*' * 10, '系统可用的中文字体', '*' * 10
# print output
zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n'))
available = mat_fonts & zh_fonts
print '*' * 10, '可用的字体', '*' * 10
for f in available:
print f
(5)根据代码中获取的字体名称,修改matplotlibrc文件(目录:/usr/lib64/python2.7/site-packages/matplotlib/mpl-data)内容 font.family : serif font.serif : 获取到的字体名, serif
参考链接:Linux下解决matplotlib中文乱码的方法