例子六、中文標籤測試html
#!/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
參考文獻api
http://hi.baidu.com/bithigher/item/b9ce6d85dc102adc98255fb7python2.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')
輸出效果:測試
對於圖例的其餘位置,以下:字體
=============== =============
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 rightspa
能夠批量添加legend,可是必須和plot對應:code
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'])
例子八、輸出圖像orm
相關函數:
#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
兩個參數控制輸出圖像的大小:
一、figure size
mpl.rcParams['figure.figsize'] = (16,9)
二、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.
改變分辨率:
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
例子九、輸出爲其餘格式
#!/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()
輸出結果:
(待續)