python3使用matplotlib画图支持中文

    python3使用matplotlib画图,因python3默认使用中unicode编码码,所以在写代码时不再需要写 plt.xlabel(u'性别')而是直接写plt.xlabel('性别');

    安装matplotlib时需要six,numpy,等组件。在windwos下最好直接下载别人编译好的。numpy-1.9.0-win32-superpack-python3.4 免得会在安装numpy时提示 “ unable to find vcvarsall.bat ”

    其它组件均可以直接pip install XXX安装

使用:

在有中文的地方加上中文相关的字体,不然会因为没有字体显示成放框,因为默认的使用的字体里没有中文的,使用例子如下:

#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib
zhfont1 = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
plt.xlabel('性别',fontproperties=zhfont1)
plt.ylabel('人数',fontproperties=zhfont1)
plt.xticks( (0,1),('男','女') ,fontproperties=zhfont1)
plt.bar(left=(0,1), height=(1,0.5), width=0.35)
plt.show()

效果:
python3使用matplotlib画图支持中文

你可能感兴趣的:(python3使用matplotlib画图支持中文)