Python Matplotlib中坐标轴标题中文及各种特殊符号的显示
(2013-11-08 20:00:24)
标签:
pythonmatplotlib中文特殊字符latex |
分类: Computer |
Matplotlib中文显示问题——用例子说明问题
#-*- coding: utf-8 -*-
from pylab import *
t = arange(-4*pi, 4*pi, 0.01)
y = sin(t)/t
plt.plot(t, y)
plt.title('test')
plt.xlabel(u'\u2103',fontproperties='SimHei')
#在这里,u'\u2103'是摄氏度,前面的u代表unicode,而引号里的内容,是通过在网上查找“℃”这一个符号的unicode编码得到的。这里的“摄氏度”是中文,要显示的话需要在后面加上fontproperties属性即可,这里设置的字体为黑体。
plt.ylabel(u'幅度',fontproperties='SimHei')#也可以直接显示中文。
plt.show()
Matplotlib中支持LaTex语法,如果要显示各种美观的数学公式和数学符号,可以稍微学习下,很有用。具体语法可参见(http://wiki.gwrite.googlecode.com/hg/misc/LaTex-EquRef.html?r=1de19067fce5484bb5c39cbd
可以这样使用:ylabel('Rice('+r'$\mu\mathrm{mol}$'+' '+'$ \mathrm{m}^{-2} \mathrm{s}^{-1}$'+')')。
中文与LaTex共同显示问题:
在坐标轴标题中同时显示中文以及带有上下标的各种数学单位,需要分两步:
import matplotlib.pyplot as plt
import numpy as np
t = 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")