问题描述: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有效