加载中…
个人资料
zxskzxsk
zxskzxsk
  • 博客等级:
  • 博客积分:0
  • 博客访问:2,004
  • 关注人气:12
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Python Matplotlib中坐标轴标题中文及各种特殊符号的显示

(2013-11-08 20:00:24)
标签:

python

matplotlib

中文

特殊字符

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=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 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")

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有