1. 程式人生 > >Python 畫圖學習入門

Python 畫圖學習入門

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.pyplot 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()


參考網址:http://www.linuxidc.com/Linux/2012-02/55296.htm


演示如何實現Matplotlib繪圖並儲存影象但不顯示圖形的方法
http://blog.csdn.net/rumswell/article/details/7342479