python matplotlib中文显示乱码解决

问题描述:python matplotlib绘图中文显示乱码
问题根源:matplotlib的默认字体并非中文字体
解决:设置为中文字体

linux下查找中文字体:fc-list :lang=zh-cn #注意:前有个空格
windows下中文字体路径:c:\windows\fonts\simsun.ttc

  • 方法1
from matplotlib.font_manager import FontProperties
font = FontProperties(fname = "/usr/share/fonts/truetype/arphic/ukai.ttc", size=14) 

plt.title(u"用户数量(Y)关于游戏消费金额(X)的分布图",fontproperties=font)
  • 方法2
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

实测:
- ubuntu 14.04 64bit + python2.7 64bit:方法1有效,方法2未测试
- win7 64bit + Anaconda 64bit(python 2.7) :方法1无效,方法2有效

参考来源:
python中matplotlib绘图中文显示问题
Ubuntu 12.04 查看自带的中文字体
Matplotlib输出中文显示问题

你可能感兴趣的:(python)