python2.7绘图中文问题

python中matplotlib默认不支持中文,绘图时出现中文会出现各种乱七八糟的问题,这里简单整理了一点。

#coding=utf-8
'''
1.python2中要输入汉字:
#coding=utf-8
2.要使用matplotlib画图:
import sys
reload(sys)
sys.setdefaultencoding('gbk')
3.plt.figure()要出现中文:
在中文前加一个u
4.坐标轴,标题,text()要出现中文:
from pylab import *
plt.xlabel(u"x轴",fontproperties='SimHei')
plt.ylabel(u"y轴",fontproperties='SimHei')
'''
import sys
reload(sys)
sys.setdefaultencoding('gbk')
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from pylab import *
# from matplotlib.font_manager import FontProperties
# zhfont = FontProperties(fname=r'c:\windows\fonts\simsun.ttc',size=20)#指定本机的汉字字体位置

plt.figure('汉字')#图像名称
x=np.linspace(-5,5,100)
y=1/(1+np.exp(-x))
plt.plot(x,y,'.')
plt.xlabel(u"x轴",fontproperties='SimHei')
plt.ylabel(u"y轴",fontproperties='SimHei')
plt.title(u"sigmoid函数",fontproperties='SimHei')
plt.grid(True)
plt.text(0,0,u"中文",fontproperties='SimHei')
# plt.savefig('result.png')
plt.show()#画好图后一定要show,不然图不会显示出来

猜你喜欢

转载自blog.csdn.net/hhhhhyyyyy8/article/details/80197518