1. subplot 函數介紹
在實踐中,經常需要在一張畫布上繪製多個圖形,subplot 函數便是 matplotlib 的 pyplot 模塊中用於繪製多圖的函數。其調用格式如下:
subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)
參數說明
- nrows, ncols, index:三個整數,用於描述子塊(子圖)的位置。如果這三個整數依次是 nrows、ncols 和 index,則子塊將在具有nrows 排和 ncols 列的網格上佔據索引位置。索引從左上角的 1 開始向右增加。
- pos :是一個三位整數,其中第一位是行數,第二位是列數,第三位是子塊的索引。即。如 235 表示圖附加子塊位置爲第 2 行第 3 列索引 5 。需要注意,所有整數必須小於10,此窗體才能工作。
- kwargs:可選參數,kwargs 用於指定繪圖的輔助特性,如標籤、線寬、線型、標記顏色等。
2. subplot 簡單繪圖示例
2.1 一張畫布繪製兩個圖
即然要在一張畫布上繪製兩個圖,那麼就需要在畫布上添加兩個子塊,然後在兩個子塊中分別繪圖,代碼示例如下:
import matplotlib.pyplot as plt
# step1:準備畫圖的數據
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [21, 27, 29, 32, 29, 28, 35, 39, 49]
# step2:手動創建一個figure對象,相當於一個空白的畫布
figure = plt.figure()
# step3:在畫布上添加兩個子塊,標定繪圖位置
axes1 = figure.add_subplot(2, 1, 1)
axes2 = figure.add_subplot(2, 1, 2)
# step4:繪圖
axes1.plot(x, y, 'ro--') # 設置線條顏色、線型、點型
axes2.plot(x, y, 'gv--') # 設置線條顏色、線型、點型
# step5:展示
plt.show()
上面代碼的運行結果:
2.2 一張畫布繪製四個圖
與在一張畫布上繪製2個圖類似,繪製4個圖則需要在畫布上添加4個子塊,然後分別在4個子塊中繪圖,代碼示例如下:
import matplotlib.pyplot as plt
# step1:準備畫圖的數據
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [21, 27, 29, 32, 29, 28, 35, 39, 49]
# step2:手動創建一個figure對象,相當於一個空白的畫布
figure = plt.figure()
# step3:在畫布上添加4個子塊,標定繪圖位置
axes1 = figure.add_subplot(2, 2, 1)
axes2 = figure.add_subplot(2, 2, 2)
axes3 = figure.add_subplot(2, 2, 3)
axes4 = figure.add_subplot(2, 2, 4)
# step4:繪圖
axes1.plot(x, y, 'ro--') # 設置線條顏色、線型、點型
axes2.plot(x, y, 'gv--') # 設置線條顏色、線型、點型
axes3.plot(x, y, 'b*--') # 設置線條顏色、線型、點型
axes4.plot(x, y, 'yp--') # 設置線條顏色、線型、點型
# step5:展示
plt.show()
上面代碼的運行結果:
2.3 一張畫布繪製3個圖
與在一張畫布上繪製2、4個圖類似,繪製3個圖則需要在畫布上添加3個座標系,不過,需要注意座標系行、列、索引的設置,代碼示例如下:
import matplotlib.pyplot as plt
# step1:準備畫圖的數據
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [21, 27, 29, 32, 29, 28, 35, 39, 49]
# step2:手動創建一個figure對象,相當於一個空白的畫布
figure = plt.figure()
# step3:在畫布上添加3個座標系,標定繪圖位置
axes1 = figure.add_subplot(2, 2, 1)
axes2 = figure.add_subplot(2, 2, 2)
axes3 = figure.add_subplot(2, 1, 2)
# step4:繪圖
axes1.plot(x, y, 'ro--') # 設置線條顏色、線型、點型
axes2.plot(x, y, 'gv--') # 設置線條顏色、線型、點型
axes3.plot(x, y, 'b*--') # 設置線條顏色、線型、點型
# step5:展示
plt.show()
上面代碼的運行結果:
3. subplot 繪圖實例
在第二節中所舉例子的繪圖數據都比較簡單,在本節筆者列舉幾個較爲複雜的例子。
3.1 繪製兩個餘弦曲線
代碼示例:
import numpy as np
import matplotlib.pyplot as plt
# step1:準備畫圖的數據
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
# step2:手動創建一個figure對象,相當於一個空白的畫布
figure = plt.figure()
plt.rcParams['axes.unicode_minus'] = False # 避免負號無法正常展示
# step3:在畫布上添加2個子塊,標定繪圖位置
axes1 = plt.subplot(2, 1, 1)
axes2 = plt.subplot(2, 1, 2)
# step4:繪圖
axes1.plot(x1, y1, 'ro-')
axes1.set_title('A tale of 2 subplots')
axes1.set_ylabel('Damped oscillation')
axes2.plot(x2, y2, 'b.-')
axes2.set_xlabel('time (s)')
axes2.set_ylabel('Undamped')
# step5:展示
plt.show()
上面代碼的運行結果:
3.2 繪製兩個散點圖
代碼示例如下:
import numpy as np
import matplotlib.pyplot as plt
# step1:準備畫圖的數據, 生成20個隨機數
x = np.random.rand(20)
y = np.random.rand(20)
# step2:手動創建一個figure對象,相當於一個空白的畫布
figure = plt.figure()
# step3:在畫布上添加2個子塊,標定繪圖位置
axes1 = plt.subplot(2, 1, 1)
axes2 = plt.subplot(2, 1, 2)
# step4:繪圖
axes1.scatter(x, y, marker='v')
axes1.set_title('random scatter example')
axes1.set_ylabel('random-y')
axes2.scatter(x, y, marker='o', color='red')
axes2.set_xlabel('random-x')
axes2.set_ylabel('random-y')
# step5:展示
plt.show()
上面代碼的運行結果: