matplotlib(五)排版布局
網(wǎng)格布局目錄
subplots()
最常見的網(wǎng)格排版方式,一次性定好所有Axes
GridSpec
復(fù)雜網(wǎng)格排列
SubplotSpec
為給定GridSpec中子圖指定位置
subplot2grid()
類似于subplot(),但使用基于0的索引并允許子圖占據(jù)多個(gè)單元格。
subplots
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspecfig1, f1_axes = plt.subplots(ncols=2, nrows=2)# 以下兩行代碼用法和下面的 GridSpec類似,注意對(duì)比 # gs_kw = dict(width_ratios=widths, height_ratios=heights) # fig5, f5_axes = plt.subplots(ncols=3, nrows=3, gridspec_kw=gs_kw)fig1.tight_layout() # 自動(dòng)緊湊布局GridSpec
1、GridSpec與add_subplot()
# 示例1 # 該示例僅僅演示,一般不會(huì)這樣子用,太冗長(zhǎng) fig2 = plt.figure() # 此句代碼不可或缺 spec2 = gridspec.GridSpec(ncols=2, nrows=2)f2_ax1 = fig2.add_subplot(spec2[0, 0]) f2_ax2 = fig2.add_subplot(spec2[0, 1]) f2_ax3 = fig2.add_subplot(spec2[1, 0]) f2_ax4 = fig2.add_subplot(spec2[1, 1])fig2.tight_layout() # 自動(dòng)緊湊布局# 示例2 fig = plt.figure() gs1 = gridspec.GridSpec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05) ax1 = fig.add_subplot(gs1[:-1, :]) ax2 = fig.add_subplot(gs1[-1, :-1]) ax3 = fig.add_subplot(gs1[-1, -1])# 示例3 # fig4 = plt.figure() widths = [2, 3, 1.5] heights = [1, 3, 2] spec4 = gridspec.GridSpec(ncols=3, nrows=3, width_ratios=widths,height_ratios=heights) for row in range(3):for col in range(3):ax = fig4.add_subplot(spec4[row, col])label = 'Width: {}\nHeight: {}'.format(widths[col], heights[row])ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')fig4.tight_layout() # 自動(dòng)緊湊布局示例2結(jié)果:
示例3結(jié)果:
2、GridSpec與SubplotSpec
fig = plt.figure() gs0 = gridspec.GridSpec(1, 2)gs00 = gridspec.GridSpecFromSubplotSpec(2, 3, subplot_spec=gs0[0]) gs01 = gridspec.GridSpecFromSubplotSpec(3, 2, subplot_spec=gs0[1])for a in range(2):for b in range(3):fig.add_subplot(gs00[a, b])fig.add_subplot(gs01[b, a])fig.tight_layout() # plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)subplot2grid
?在常規(guī)網(wǎng)格內(nèi)的特定位置創(chuàng)建軸
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 = plt.figure()ax1 = plt.subplot2grid((3, 3), (0, 0)) ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2) ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2) ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)example_plot(ax1) example_plot(ax2) example_plot(ax3) example_plot(ax4)plt.tight_layout() # tight_layout() only considers ticklabels, axis labels, and titles. Thus, other artists may be clipped and also may overlaptight_Layout
??tight_layout在上面示例中已經(jīng)出現(xiàn)過,在這里做一個(gè)補(bǔ)充說明,tight_layout自動(dòng)調(diào)整subplot(s)參數(shù),以便subplot(s)適應(yīng)圖形區(qū)域。這是一個(gè)實(shí)驗(yàn)性功能,在某些情況下可能無效,而且它僅對(duì)ticklabels, axis labels, titles有效。作圖時(shí)我們可能會(huì)遇到這種問題,發(fā)現(xiàn)部分title、ticks、label被截?cái)嗔?#xff0c;如下圖所示:
??這種情況下,參數(shù)我們就可以用tight_layout來進(jìn)行微調(diào),但是請(qǐng)注意,matplotlib.pyplot.tight_layout()僅在調(diào)用時(shí)調(diào)整subplot 。為了在每次重繪圖形時(shí)執(zhí)行此調(diào)整,您可以調(diào)用fig.set_tight_layout(True),或者等效地將figure.autolayout rcParam設(shè)置為True。 tight_layout還可以用來調(diào)整標(biāo)題圖形重疊的情況,如下圖所示:
??
??
# 這樣這個(gè)函數(shù)就類似于 adjust_subplots plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)總結(jié)
以上是生活随笔為你收集整理的matplotlib(五)排版布局的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matplotlib(四)核心模式以及注
- 下一篇: Linux 后台运行程序方法总结