共計(jì) 5702 個(gè)字符,預(yù)計(jì)需要花費(fèi) 15 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下 matplotlib subplot 子圖怎么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
總括
MATLAB 和 pyplot 有當(dāng)前的圖形(figure)和當(dāng)前的軸(axes)的概念,所有的作圖命令都是對當(dāng)前的對象作用。可以通過 gca() 獲得當(dāng)前的 axes(軸),通過 gcf() 獲得當(dāng)前的圖形(figure)
import numpy as npimport matplotlib.pyplot as pltdef f(t):return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), bo , t2, f(t2), k )
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), r-- )
plt.show()
如果不指定 figure() 的軸,figure(1) 命令默認(rèn)會(huì)被建立,同樣的如果你不指定 subplot(numrows, numcols, fignum) 的軸,subplot(111) 也會(huì)自動(dòng)建立。
import matplotlib.pyplot as plt
plt.figure(1) # 創(chuàng)建第一個(gè)畫板(figure)plt.subplot(211) # 第一個(gè)畫板的第一個(gè)子圖 plt.plot([1, 2, 3])
plt.subplot(212) # 第二個(gè)畫板的第二個(gè)子圖 plt.plot([4, 5, 6])
plt.figure(2) #創(chuàng)建第二個(gè)畫板 plt.plot([4, 5, 6]) # 默認(rèn)子圖命令是 subplot(111)plt.figure(1) # 調(diào)取畫板 1; subplot(212) 仍然被調(diào)用中 plt.subplot(211) # 調(diào)用 subplot(211)plt.title(Easy as 1, 2, 3) # 做出 211 的標(biāo)題
subplot() 是將整個(gè) figure 均等分割,而 axes() 則可以在 figure 上畫圖。
import matplotlib.pyplot as plt
import numpy as np# 創(chuàng)建數(shù)據(jù) dt = 0.001t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:1000]/0.05) # impulse responsex = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)]*dt # colored noise# 默認(rèn)主軸圖 axes 是 subplot(111)plt.plot(t, s)
plt.axis([0, 1, 1.1*np.amin(s), 2*np.amax(s)])
plt.xlabel(time (s) )
plt.ylabel(current (nA) )
plt.title(Gaussian colored noise)# 內(nèi)嵌圖 a = plt.axes([.65, .6, .2, .2], facecolor= y )
n, bins, patches = plt.hist(s, 400, normed=1)
plt.title(Probability)
plt.xticks([])
plt.yticks([])# 另外一個(gè)內(nèi)嵌圖 a = plt.axes([0.2, 0.6, .2, .2], facecolor= y )
plt.plot(t[:len(r)], r)
plt.title(Impulse response)
plt.xlim(0, 0.2)
plt.xticks([])
plt.yticks([])
plt.show()
你可以通過 clf() 清空當(dāng)前的圖板(figure),通過 cla() 來清理當(dāng)前的軸(axes)。你需要特別注意的是記得使用 close() 關(guān)閉當(dāng)前 figure 畫板
通過 GridSpec 來定制 Subplot 的坐標(biāo)
GridSpec 指定子圖所放置的幾何網(wǎng)格。
SubplotSpec 在 GridSpec 中指定子圖(subplot)的位置。
subplot2grid 類似于“pyplot.subplot”,但是它從 0 開始索引
ax = plt.subplot2grid((2,2),(0, 0))
ax = plt.subplot(2,2,1)
以上兩行的子圖(subplot)命令是相同的。subplot2grid 使用的命令類似于 HTML 語言。
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)ax4 = plt.subplot2grid((3,3), (2, 0))ax5 = plt.subplot2grid((3,3), (2, 1))
使用 GridSpec 和 SubplotSpec
ax = plt.subplot2grid((2,2),(0, 0))
相當(dāng)于
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])
一個(gè) gridspec 實(shí)例提供給了類似數(shù)組的索引來返回 SubplotSpec 實(shí)例,所以我們可以使用切片(slice)來合并單元格。
gs = gridspec.GridSpec(3, 3)ax1 = plt.subplot(gs[0, :])ax2 = plt.subplot(gs[1,:-1])ax3 = plt.subplot(gs[1:, -1])ax4 = plt.subplot(gs[-1,0])ax5 = plt.subplot(gs[-1,-2])
調(diào)整 GridSpec 圖層
當(dāng) GridSpec 被使用后,你可以調(diào)整子圖(subplot)的參數(shù)。這個(gè)類似于 subplot_adjust,但是它只作用于 GridSpec 實(shí)例。
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])
gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])
在 subplotSpec 中嵌套 GridSpec
gs0 = gridspec.GridSpec(1, 2)gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1])
下圖是一個(gè) 3 * 3 方格,嵌套在 4 * 4 方格中的例子
調(diào)整 GridSpec 的尺寸
默認(rèn)的,GridSpec 會(huì)創(chuàng)建相同尺寸的單元格。你可以調(diào)整相關(guān)的行與列的高度和寬度。注意,絕對值是不起作用的,相對值才起作用。
gs = gridspec.GridSpec(2, 2,
width_ratios=[1,2],
height_ratios=[4,1]
)
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax3 = plt.subplot(gs[2])
ax4 = plt.subplot(gs[3])
調(diào)整圖層
tigh_layout 自動(dòng)調(diào)整子圖(subplot)參數(shù)來適應(yīng)畫板(figure)的區(qū)域。它只會(huì)檢查刻度標(biāo)簽(ticklabel),坐標(biāo)軸標(biāo)簽(axis label),標(biāo)題(title)。
軸(axes)包括子圖(subplot)被畫板(figure)的坐標(biāo)指定。所以一些標(biāo)簽會(huì)超越畫板(figure)的范圍。
plt.rcParams[savefig.facecolor] = 0.8 def example_plot(ax, fontsize=12):ax.plot([1, 2])
ax.locator_params(nbins=3)
ax.set_xlabel(x-label , fontsize=fontsize)
ax.set_ylabel(y-label , fontsize=fontsize)
ax.set_title(Title , fontsize=fontsize)
plt.close(all)
fig, ax = plt.subplots()
example_plot(ax, fontsize=24)
對于子圖(subplot)可以通過調(diào)整 subplot 參數(shù)解決這個(gè)問題。Matplotlib v1.1 引進(jìn)了一個(gè)新的命令 tight_layout() 自動(dòng)的解決這個(gè)問題
plt.tight_layout()
很多子圖的情況
plt.close(all)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)example_plot(ax1) example_plot(ax2) example_plot(ax3) example_plot(ax4)
plt.tight_layout()
tight_layout() 含有 pad,w_pad 和 h_pad
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
在 GridSpec 中使用 tight_layout()
GridSpec 擁有它自己的 tight_layout() 方法
plt.close(all)fig = plt.figure()
import matplotlib.gridspec as gridspecgs1 = gridspec.GridSpec(2, 1)ax1 = fig.add_subplot(gs1[0])ax2 = fig.add_subplot(gs1[1])
example_plot(ax1)
example_plot(ax2)
gs1.tight_layout(fig)
你可以指定一個(gè)可選擇的方形(rect)參數(shù)來指定子圖(subplot)到畫板(figure)的距離
這個(gè)還可以應(yīng)用到復(fù)合的 gridspecs 中
gs2 = gridspec.GridSpec(3, 1)
for ss in gs2:
ax = fig.add_subplot(ss)
example_plot(ax)
ax.set_title( )
ax.set_xlabel( )
ax.set_xlabel(x-label , fontsize=12)
gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.5)
在 AxesGrid1 中使用 tight_layout()
plt.close(all)
fig = plt.figure()from mpl_toolkits.axes_grid1 import Grid
grid = Grid(fig, rect=111, nrows_ncols=(2,2),
axes_pad=0.25, label_mode= L ,
)for ax in grid:
example_plot(ax)
ax.title.set_visible(False)
plt.tight_layout()
在 colorbar 中使用 tight_layout()
colorbar 是 Axes 的實(shí)例,而不是 Subplot 的實(shí)例,所以 tight_layout 不會(huì)起作用,在 matplotlib v1.1 中,你把 colorbar 作為一個(gè) subplot 來使用 gridspec。
plt.close(all)
arr = np.arange(100).reshape((10,10))
fig = plt.figure(figsize=(4, 4))
im = plt.imshow(arr, interpolation= none)
plt.colorbar(im, use_gridspec=True)
plt.tight_layout()
另外一個(gè)方法是,使用 AxesGrid1 工具箱為 colorbar 創(chuàng)建一個(gè)軸 Axes
plt.close(all)
arr = np.arange(100).reshape((10,10))
fig = plt.figure(figsize=(4, 4))
im = plt.imshow(arr, interpolation= none)from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes(right , 5% , pad= 3%)
plt.colorbar(im, cax=cax)
plt.tight_layout()
看完了這篇文章,相信你對“matplotlib subplot 子圖怎么用”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道,感謝各位的閱讀!