Matplotlib 繪制多圖

2022-07-04 16:27 更新

Matplotlib 繪制多圖

我們可以使用 pyplot 中的 subplot() 和 subplots() 方法來(lái)繪制多個(gè)子圖。

subplot() 方法在繪圖時(shí)需要指定位置,subplots() 方法可以一次生成多個(gè),在調(diào)用時(shí)只需要調(diào)用生成對(duì)象的 ax 即可。

subplot

subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(**kwargs)
subplot(ax)

以上函數(shù)將整個(gè)繪圖區(qū)域分成 nrows 行和 ncols 列,然后從左到右,從上到下的順序?qū)γ總€(gè)子區(qū)域進(jìn)行編號(hào) 1...N ,左上的子區(qū)域的編號(hào)為 1、右下的區(qū)域編號(hào)為 N,編號(hào)可以通過(guò)參數(shù) index 來(lái)設(shè)置。

設(shè)置 numRows = 1,numCols = 2,就是將圖表繪制成 1x2 的圖片區(qū)域, 對(duì)應(yīng)的坐標(biāo)為:

(1, 1), (1, 2)

plotNum = 1, 表示的坐標(biāo)為(1, 1), 即第一行第一列的子圖。

plotNum = 2, 表示的坐標(biāo)為(1, 2), 即第一行第二列的子圖。

實(shí)例

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("W3Cschool subplot Test")
plt.show()

顯示結(jié)果如下:


設(shè)置 numRows = 2,numCols = 2,就是將圖表繪制成 2x2 的圖片區(qū)域, 對(duì)應(yīng)的坐標(biāo)為:

(1, 1), (1, 2)
(2, 1), (2, 2)

plotNum = 1, 表示的坐標(biāo)為(1, 1), 即第一行第一列的子圖。

plotNum = 2, 表示的坐標(biāo)為(1, 2), 即第一行第二列的子圖。

plotNum = 3, 表示的坐標(biāo)為(2, 1), 即第二行第一列的子圖。

plotNum = 4, 表示的坐標(biāo)為(2, 2), 即第二行第二列的子圖。

實(shí)例

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("W3Cschool subplot Test")
plt.show()

顯示結(jié)果如下:


subplots()

subplots() 方法語(yǔ)法格式如下:

matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
參數(shù)說(shuō)明:
  • nrows:默認(rèn)為 1,設(shè)置圖表的行數(shù)。
  • ncols:默認(rèn)為 1,設(shè)置圖表的列數(shù)。
  • sharex、sharey:設(shè)置 x、y 軸是否共享屬性,默認(rèn)為 false,可設(shè)置為 'none'、'all'、'row' 或 'col'。 False 或 none 每個(gè)子圖的 x 軸或 y 軸都是獨(dú)立的,True 或 'all':所有子圖共享 x 軸或 y 軸,'row' 設(shè)置每個(gè)子圖行共享一個(gè) x 軸或 y 軸,'col':設(shè)置每個(gè)子圖列共享一個(gè) x 軸或 y 軸。
  • squeeze:布爾值,默認(rèn)為 True,表示額外的維度從返回的 Axes(軸)對(duì)象中擠出,對(duì)于 N*1 或 1*N 個(gè)子圖,返回一個(gè) 1 維數(shù)組,對(duì)于 N*M,N>1 和 M>1 返回一個(gè) 2 維數(shù)組。如果設(shè)置為 False,則不進(jìn)行擠壓操作,返回一個(gè)元素為 Axes 實(shí)例的2維數(shù)組,即使它最終是1x1。
  • subplot_kw:可選,字典類(lèi)型。把字典的關(guān)鍵字傳遞給 add_subplot() 來(lái)創(chuàng)建每個(gè)子圖。
  • gridspec_kw:可選,字典類(lèi)型。把字典的關(guān)鍵字傳遞給 GridSpec 構(gòu)造函數(shù)創(chuàng)建子圖放在網(wǎng)格里(grid)。
  • **fig_kw:把詳細(xì)的關(guān)鍵字參數(shù)傳給 figure() 函數(shù)。

實(shí)例

import matplotlib.pyplot as plt
import numpy as np

# 創(chuàng)建一些測(cè)試數(shù)據(jù) -- 圖1
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

# 創(chuàng)建一個(gè)畫(huà)像和子圖 -- 圖2
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')

# 創(chuàng)建兩個(gè)子圖 -- 圖3
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)

# 創(chuàng)建四個(gè)子圖 -- 圖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')

# 這個(gè)也是共享 x 軸和 y 軸
plt.subplots(2, 2, sharex=True, sharey=True)

# 創(chuàng)建標(biāo)識(shí)為 10 的圖,已經(jīng)存在的則刪除
fig, ax = plt.subplots(num=10, clear=True)

plt.show()

部分圖表顯示結(jié)果如下:

圖1

圖2

圖3

圖4


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)