背景:数据画图,并且要求使用中文。
环境:使用公司内部Jupyter club。
解决:
先导入字体:zhfont1 = matplotlib.font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Light.ttc')
然后
fig = plt.figure(figsize=(10,5)) #定义图大小
ax = fig.add_subplot(111) #画布分为1行1列,该图画在第1个子图中
line1 = ax.plot(x,y,label=u'小明') #label定义图标,注意,图标的中文加单引号引用,并且前面加u。
lines = line1 #这是只有一条线的情况,多条线就加起来。
labs = [l.get_label() for l in lines] #取图标
ax.legend(lines, labs, loc='best',prop=zhfont1) #定义图标,注意zhfont1是字体,prop是加字体的参数。loc是图标在图里的位置参数。
plt.xlabel(u'日期', fontproperties=zhfont1) #x轴名称。与lengend参数不同,fontproperties是坐标轴名称和图表标题改变字体的参数。
plt.title(u'预览页到下单提交页用户累积数转化率', fontproperties=zhfont1) #图表标题
plt.show()