matplotlib显示中文

在使用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)

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

# ********** 可用的字体 *********
# Droid Sans Fallback

我这系统显示的只支持Droid Sans Fallbackfont.sans-serif,那在使用的时候指定font.sans-serif就可以正常显示了。


import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['Droid Sans Fallback']

现在就正常显示中文了。


相关推荐


评论(0条)

暂时还没有评论,第一个来评论吧!


我要发表看法

引用   粗体   链接   缩进  

最近编辑

热门标签