matplotlib(五)排版布局
生活随笔
收集整理的這篇文章主要介紹了
matplotlib(五)排版布局
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網格布局目錄
subplots()
最常見的網格排版方式,一次性定好所有Axes
GridSpec
復雜網格排列
SubplotSpec
為給定GridSpec中子圖指定位置
subplot2grid()
類似于subplot(),但使用基于0的索引并允許子圖占據多個單元格。
subplots
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspecfig1, f1_axes = plt.subplots(ncols=2, nrows=2)# 以下兩行代碼用法和下面的 GridSpec類似,注意對比 # 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() # 自動緊湊布局GridSpec
1、GridSpec與add_subplot()
# 示例1 # 該示例僅僅演示,一般不會這樣子用,太冗長 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() # 自動緊湊布局# 示例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() # 自動緊湊布局示例2結果:
示例3結果:
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
?在常規網格內的特定位置創建軸
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在上面示例中已經出現過,在這里做一個補充說明,tight_layout自動調整subplot(s)參數,以便subplot(s)適應圖形區域。這是一個實驗性功能,在某些情況下可能無效,而且它僅對ticklabels, axis labels, titles有效。作圖時我們可能會遇到這種問題,發現部分title、ticks、label被截斷了,如下圖所示:
??這種情況下,參數我們就可以用tight_layout來進行微調,但是請注意,matplotlib.pyplot.tight_layout()僅在調用時調整subplot 。為了在每次重繪圖形時執行此調整,您可以調用fig.set_tight_layout(True),或者等效地將figure.autolayout rcParam設置為True。 tight_layout還可以用來調整標題圖形重疊的情況,如下圖所示:
??
??
# 這樣這個函數就類似于 adjust_subplots plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)總結
以上是生活随笔為你收集整理的matplotlib(五)排版布局的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matplotlib(四)核心模式以及注
- 下一篇: Pandas处理数据缺失值