Python:使用matplotlib绘制图表 


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.pyplo 
t as plt   

3.   

4.plt.plot([10, 20, 30])   

5.plt.xlabel('tiems')   

6.plt.ylabel('numbers')   

7.plt.show()




2.饼图: 

代码: 

1.#!/usr/bin/env python    

2.# -*- coding: utf-8 -*-    

3.   

4.from pylab import *   

5.   

6.# make a square figure and axes    

7.figure(1, figsize=(6,6))   

8.ax = axes([0.1, 0.1, 0.8, 0.8])   

9.   

10.labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'   

11.fracs = [15,30,45, 10]   

12.   

13.explode=(0, 0.05, 0, 0)   

14.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)   

15.title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})   

16.savefig('D:\\pie.png')   

17.show()




3、使用numpy库函数: 

代码: 

1.#!/usr/bin/env python    

2.# -*- coding: utf-8 -*-    

3.   

4.import numpy as np   

5.import matplotlib.pyplot as plt   

6.   

7.t = np.arange(0.0, 1.01, 0.01)   

8.s = np.sin(2*2*np.pi*t)   

9.   

10.plt.fill(t, s*np.exp(-5*t), 'r')   

11.plt.grid(True)   

12.   

13.#保存为PDF格式,也可保存为PNG等图形格式    

14.plt.savefig('D:\\test.pdf')   

15.plt.show()