本文实例讲述了Python使用Matplotlib模块时坐标轴标题中文及各种特殊符号显示方法。分享给大家供大家参考,具体如下:
Matplotlib中文显示问题——用例子说明问题
#-*- coding: utf-8 -*-from pylab import *t = arange(-4*pi, 4*pi, 0.01)y = sin(t)/tplt.plot(t, y)plt.title('/hg/misc/LaTex-EquRef.html?r=1de19067fce5484bb5c39cbd049f6a47f7d8a2e9)可以这样使用:
复制代码 代码如下:ylabel('Rice('+r'$\mu\mathrm{mol}$'+' '+'$ \mathrm{m}^{-2} \mathrm{s}^{-1}$'+')')
中文与LaTex共同显示问题:
在坐标轴标题中同时显示中文以及带有上下标的各种数学单位,需要分两步:
1、根据上述显示中文的方法,先将中文标题加上;
2、对于单位,使用text函数进行添加,text函数用法见(http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text)。
import matplotlib.pyplot as pltimport numpy as npt = np.linspace(0, 10, 1000)y = np.sin(t)plt.plot(t, y,label=u'正弦曲线 (m)')plt.xlabel(u"时间", fontproperties='SimHei')plt.ylabel(u"振幅", fontproperties='SimHei')plt.title(u"正弦波", fontproperties='SimHei')# 添加单位t=plt.text(6.25, -1.14,r'$(\mu\mathrm{mol}$'+' '+'$ \mathrm{m}^{-2} \mathrm{s}^{-1})$',fontsize=15, horizontalalignment='center',verticalalignment='center')#在这里设置是text的旋转,0为水平,90为竖直t.set_rotation(0)# legend中显示中文plt.legend(prop={'family':'SimHei','size':15})plt.savefig("C:\\Users\\Administrator\\Desktop\\test.png")更多关于Python相关内容可查看本站专题:《Python数学运算技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。