歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python:使用matplotlib繪制圖表

Python:使用matplotlib繪制圖表

日期:2017/3/1 10:28:59   编辑:Linux編程
看了一下使用python繪制圖表的方法,有個強大的類庫matplotlib,可以制作出高質量的2D和3D圖形,先記錄一下,以後慢慢學習。

matplotlib下載及API手冊地址:http://sourceforge.net/projects/matplotlib/files/matplotlib/

數學庫numpy下載及API手冊地址:http://www.scipy.org/Download

幾個繪圖的例子,來自API手冊:

1、最簡單的圖:

代碼:

  1. #!/usr/bin/env python
  2. import matplotlib.pyplot as plt
  3. plt.plot([10, 20, 30])
  4. plt.xlabel('tiems')
  5. plt.ylabel('numbers')
  6. plt.show()

測試:

2.餅圖:

代碼:

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from pylab import *
  4. # make a square figure and axes
  5. figure(1, figsize=(6,6))
  6. ax = axes([0.1, 0.1, 0.8, 0.8])
  7. labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
  8. fracs = [15,30,45, 10]
  9. explode=(0, 0.05, 0, 0)
  10. pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
  11. title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})
  12. savefig('D:\\pie.png')
  13. show()

測試:

3、使用numpy庫函數:

代碼:

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. t = np.arange(0.0, 1.01, 0.01)
  6. s = np.sin(2*2*np.pi*t)
  7. plt.fill(t, s*np.exp(-5*t), 'r')
  8. plt.grid(True)
  9. #保存為PDF格式,也可保存為PNG等圖形格式
  10. plt.savefig('D:\\test.pdf')
  11. plt.show()

測試:

Copyright © Linux教程網 All Rights Reserved