Python之matplotlib學習(二)


例子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')  

測試效果:

參考文獻

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')

輸出效果:

對於圖例的其他位置,如下:

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

輸出結果:

(待續)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM