例子6、中文标签测试

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*- import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm fontpath = '/usr/share/fonts/chinese/TrueType/ukai.ttf'
myfont = fm.FontProperties(fname=fontpath)
#定义一个myfont变量, myfont = matplotlib.font_manager.FontProperties(fname=fontpath); fontpath就是字体文件的路径
x = np.arange(1,5)
plt.plot(x,x*3.0,x,x*1.5,x,x/3.0) plt.grid(True) #添加背景方格
plt.xlabel(u'X轴',fontproperties=myfont)
plt.ylabel(u'Y轴',fontproperties=myfont)
plt.title(u'中文测试',fontproperties=myfont) plt.savefig('test3.png')

测试效果:

Python之matplotlib学习(二)-LMLPHP

参考文献

http://hi.baidu.com/bithigher/item/b9ce6d85dc102adc98255fb7

例子7、添加图例

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*- import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm fontpath = '/usr/share/fonts/chinese/TrueType/ukai.ttf'
myfont = fm.FontProperties(fname=fontpath) x = np.arange(1,5)
#设置legend,图例说明
plt.plot(x, x*1.5, label = "Normal")
plt.plot(x, x*3.0, label = "Fast")
plt.plot(x, x/3.0, label = "Slow") plt.grid(True)
plt.xlabel(u'X轴',fontproperties=myfont)
plt.ylabel(u'Y轴',fontproperties=myfont)
plt.title(u'中文测试',fontproperties=myfont) #Place a legend on the current axes
#设置图例显示的位置
plt.legend(loc='upper left')
#Save the current figure
plt.savefig('test4.png')

输出效果:

Python之matplotlib学习(二)-LMLPHP

对于图例的其他位置,如下:

          ===============   =============
Location String Location Code
=============== =============
'best' 0
'upper right' 1
'upper left' 2
'lower left' 3
'lower right' 4
'right' 5
'center left' 6
'center right' 7
'lower center' 8
'upper center' 9
'center' 10
=============== =============

可以选择best,默认upper right

可以批量添加legend,但是必须和plot对应:

In [4]: plt.plot(x, x*1.5)
In [5]: plt.plot(x, x*3.0)
In [6]: plt.plot(x, x/3.0)
In [7]: plt.legend(['Normal', 'Fast', 'Slow'])

例子8、输出图像

相关函数:

#Save the current figure
plt.savefig('test4.png')

输出图像的格式:

[root@typhoeus79 20131114]# file test4.png
test4.png: PNG image data, 800 x 600, 8-bit/color RGBA, non-interlaced

并且按照文件扩展名来区分,默认分辨率是800*600

两个参数控制输出图像的大小:

1、figure size

mpl.rcParams['figure.figsize'] = (16,9)

2、DPI

In [1]: import matplotlib as mpl

In [2]: mpl.rcParams['figure.figsize']
Out[2]: [8.0, 6.0] In [3]: mpl.rcParams['savefig.dpi']
Out[3]: 100
matplotlib.rcParams
An instance of RcParams for handling default matplotlib values.

http://matplotlib.org/1.3.1/api/matplotlib_configuration_api.html?highlight=rcparams#matplotlib.RcParams

改变分辨率:

plt.savefig('test4.png',dpi=200)
[root@typhoeus79 20131114]# file test4.png
test4.png: PNG image data, 1600 x 1200, 8-bit/color RGBA, non-interlaced

例子9、输出为其他格式

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*-
import matplotlib as mpl
mpl.use('Agg')#before importing pyplot
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm fontpath = '/usr/share/fonts/chinese/TrueType/ukai.ttf'
myfont = fm.FontProperties(fname=fontpath) x = np.arange(1,5)
plt.plot(x, x*1.5, label = "Normal")
plt.plot(x, x*3.0, label = "Fast")
plt.plot(x, x/3.0, label = "Slow") plt.grid(True)
plt.xlabel(u'X轴',fontproperties=myfont)
plt.ylabel(u'Y轴',fontproperties=myfont)
plt.title(u'中文测试',fontproperties=myfont) plt.legend(loc='best') #以文件名后缀作为区分
plt.savefig('test4.pdf',dpi=500)

或者PS,SVG其他格式都可以。

例子10 使用GTK

>>> import matplotlib as mpl
>>> mpl.use('GTKAgg') # to use GTK UI
>>>
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,3,2,4])
[<matplotlib.lines.Line2D object at 0x02ED3630>]
>>> plt.show()

输出结果:

Python之matplotlib学习(二)-LMLPHP

(待续)

05-11 19:35