1.安装中文字体
git clone https://github.com/tracyone/program_font && cd program_font && ./install.sh
2.拷贝ttf字体文件到matplotlib字体库
cp /usr/share/fonts/MyFonts/*.ttf /usr/share/matplotlib/mpl-data/fonts/ttf/
3.删除matplotlib缓存列表
rm -r ~/.cache/matplotlib
4.设置中文代码
#画图前调用set_matplot_zh_font()即可显示中文
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontManager
from pylab import mpl
import subprocess
def get_matplot_zh_font():
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"',sh$
output = output.decode('utf-8')
zh_fonts = set(f.split(',',1)[0] for f in output.split('\n'))
available = list(mat_fonts & zh_fonts)
print '*' * 10,'可用字体','*' * 10
for f in available:
print f
return available
def set_matplot_zh_font():
available = get_matplot_zh_font()
if len(available) > 0:
mpl.rcParams['font.sans-serif'] = [available[0]]
mpl.rcParams['axes.unicode_minus'] = False