Matplotlib 繪制多圖
我們可以使用 pyplot 中的 subplot() 和 subplots() 方法來繪制多個子圖。
subplot() 方法在繪圖時需要指定位置,subplots() 方法可以一次生成多個,在調用時只需要調用生成對象的 ax 即可。
subplot
subplot(nrows, ncols, index, **kwargs) subplot(pos, **kwargs) subplot(**kwargs) subplot(ax)
以上函數將整個繪圖區域分成 nrows 行和 ncols 列,然后從左到右,從上到下的順序對每個子區域進行編號 1...N ,左上的子區域的編號為 1、右下的區域編號為 N,編號可以通過參數 index 來設置。
設置 numRows = 1,numCols = 2,就是將圖表繪制成 1x2 的圖片區域, 對應的坐標為:
(1, 1), (1, 2)
plotNum = 1, 表示的坐標為(1, 1), 即第一行第一列的子圖。
plotNum = 2, 表示的坐標為(1, 2), 即第一行第二列的子圖。
實例
import matplotlib.pyplot as plt import numpy as np #plot 1: xpoints = np.array([0, 6]) ypoints = np.array([0, 100]) plt.subplot(1, 2, 1) plt.plot(xpoints,ypoints) plt.title("plot 1") #plot 2: x = np.array([1, 2, 3, 4]) y = np.array([1, 4, 9, 16]) plt.subplot(1, 2, 2) plt.plot(x,y) plt.title("plot 2") plt.suptitle("subplot Test") plt.show()
顯示結果如下:
設置 numRows = 2,numCols = 2,就是將圖表繪制成 2x2 的圖片區域, 對應的坐標為:
(1, 1), (1, 2) (2, 1), (2, 2)
plotNum = 1, 表示的坐標為(1, 1), 即第一行第一列的子圖。
plotNum = 2, 表示的坐標為(1, 2), 即第一行第二列的子圖。
plotNum = 3, 表示的坐標為(2, 1), 即第二行第一列的子圖。
plotNum = 4, 表示的坐標為(2, 2), 即第二行第二列的子圖。
實例
import matplotlib.pyplot as plt import numpy as np #plot 1: x = np.array([0, 6]) y = np.array([0, 100]) plt.subplot(2, 2, 1) plt.plot(x,y) plt.title("plot 1") #plot 2: x = np.array([1, 2, 3, 4]) y = np.array([1, 4, 9, 16]) plt.subplot(2, 2, 2) plt.plot(x,y) plt.title("plot 2") #plot 3: x = np.array([1, 2, 3, 4]) y = np.array([3, 5, 7, 9]) plt.subplot(2, 2, 3) plt.plot(x,y) plt.title("plot 3") #plot 4: x = np.array([1, 2, 3, 4]) y = np.array([4, 5, 6, 7]) plt.subplot(2, 2, 4) plt.plot(x,y) plt.title("plot 4") plt.suptitle("RUNOOB subplot Test") plt.show()
顯示結果如下:
subplots()
subplots() 方法語法格式如下:
matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
參數說明:
- nrows:默認為 1,設置圖表的行數。
- ncols:默認為 1,設置圖表的列數。
- sharex、sharey:設置 x、y 軸是否共享屬性,默認為 false,可設置為 'none'、'all'、'row' 或 'col'。 False 或 none 每個子圖的 x 軸或 y 軸都是獨立的,True 或 'all':所有子圖共享 x 軸或 y 軸,'row' 設置每個子圖行共享一個 x 軸或 y 軸,'col':設置每個子圖列共享一個 x 軸或 y 軸。
- squeeze:布爾值,默認為 True,表示額外的維度從返回的 Axes(軸)對象中擠出,對於 N*1 或 1*N 個子圖,返回一個 1 維數組,對於 N*M,N>1 和 M>1 返回一個 2 維數組。如果設置為 False,則不進行擠壓操作,返回一個元素為 Axes 實例的2維數組,即使它最終是1x1。
- subplot_kw:可選,字典類型。把字典的關鍵字傳遞給 add_subplot() 來創建每個子圖。
- gridspec_kw:可選,字典類型。把字典的關鍵字傳遞給 GridSpec 構造函數創建子圖放在網格里(grid)。
- **fig_kw:把詳細的關鍵字參數傳給 figure() 函數。
實例
import matplotlib.pyplot as plt import numpy as np # 創建一些測試數據 -- 圖1 x = np.linspace(0, 2*np.pi, 400) y = np.sin(x**2) # 創建一個畫像和子圖 -- 圖2 fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('Simple plot') # 創建兩個子圖 -- 圖3 f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot(x, y) ax1.set_title('Sharing Y axis') ax2.scatter(x, y) # 創建四個子圖 -- 圖4 fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar")) axs[0, 0].plot(x, y) axs[1, 1].scatter(x, y) # 共享 x 軸 plt.subplots(2, 2, sharex='col') # 共享 y 軸 plt.subplots(2, 2, sharey='row') # 共享 x 軸和 y 軸 plt.subplots(2, 2, sharex='all', sharey='all') # 這個也是共享 x 軸和 y 軸 plt.subplots(2, 2, sharex=True, sharey=True) # 創建10 張圖,已經存在的則刪除 fig, ax = plt.subplots(num=10, clear=True) plt.show()
部分圖表顯示結果如下:
圖1
圖2
圖3
圖4
REF
https://www.runoob.com/matplotlib/matplotlib-subplots.html