首页 > 代码库 > matplotlib图例中文乱码解决

matplotlib图例中文乱码解决

#! /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)
#print(mat_fonts)
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.decode(‘utf-8‘).split(‘\n‘))
available = mat_fonts & zh_fonts
print (‘*‘ * 10, u‘可用的字体‘, ‘*‘ * 10)
for f in available:  
    print (f)

 

修改你的matplotlib的配置文件matplotlibrc,

anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc

这一部分,将#font.family : sans-serif 修改为
font.family :  ‘Droid Sans Fallback‘

matplotlib图例中文乱码解决