15_多子图-Subplot、Subplot: 使用多个figures和 axes、替代解决方案:
15.多子圖-Subplot
15.1.Subplot: 使用多個figures和 axes
15.2.替代解決方案:
15.多子圖-Subplot
Matplotlib繪圖時一個常見問題是如何在一個圖中包含多個圖。即如何實現在一個窗口中有多個圖形,每個圖形都出現在子圖中。
我們將使用兩種不同的方法來實現這一目標:
?subplot
?gridspec
15.1.Subplot: 使用多個figures和 axes
subplot及其參數:
subplot(nrows, ncols, plot_number)
如果將subplot應用于一個figure,則該figure將在概念上分為nrows * ncols個子軸。
參數plot_number標識函數調用創建的subplot。 plot_number的范圍可以從1到最大nrows * ncols。
如果三個參數的值小于10,則可以使用一個int參數調用函數subplot,其中百位數表示nrows,十位數表示ncols,個位數表示plot_number。 這意味著:可以寫subplot(234)來代替subplot(2, 3, 4) 。
在下面的示例中,我們實現一個2x2網格的兩個子圖:
import matplotlib.pyplot as plt plt.figure(figsize=(6, 4)) plt.subplot(221) # equivalent to: plt.subplot(2, 2, 1) plt.text(0.5, # x coordinate, 0 leftmost positioned, 1 rightmost0.5, # y coordinate, 0 topmost positioned, 1 bottommost'subplot(2,2,1)', # the text which will be printed (2,2,1即:2行,2列,第1個個位置)horizontalalignment='center', # shortcut 'ha'verticalalignment='center', # shortcut 'va'fontsize=20, # can be named 'font' as wellalpha=.7 # float (0.0 transparent through 1.0 opaque)) python_course_green = "#90EE90" # 224即:2行,2列,第4個位置 plt.subplot(224, facecolor=python_course_green) plt.text(0.5, 0.5,'subplot(2,2,4)',ha='center', va='center',fontsize=20,color="b")plt.show()如果不需要在軸上設置刻度,可以將它們設置為空元組,并添加以下代碼行:
plt.xticks(()) import matplotlib.pyplot as plt plt.figure(figsize=(6, 4)) plt.subplot(221) # equivalent to: plt.subplot(2, 2, 1) plt.xticks(()) plt.yticks(()) plt.text(0.5, # x coordinate, 0 leftmost positioned, 1 rightmost0.5, # y coordinate, 0 topmost positioned, 1 bottommost'subplot(2,2,1)', # the text which will be printedhorizontalalignment='center', # shortcut 'ha'verticalalignment='center', # shortcut 'va'fontsize=20, # can be named 'font' as wellalpha=.7 # float (0.0 transparent through 1.0 opaque)) python_course_green = "#90EE90" plt.subplot(224, facecolor=python_course_green) plt.xticks(()) plt.yticks(()) plt.text(0.5, 0.5,'subplot(2,2,4)',ha='center', va='center',fontsize=20,color="b") plt.show()我們還可以使用Figure類的實例即使用面向對象的方法。
下面重寫前面的例子來展示。 在這種情況下,將add_subplot方法應用于Figure對象。
讓我們再次去除刻度。 這次不能使用plt.xticks(())和plt.yticks(())。 而必須使用set_xticks(())和set_yticks(())方法。
import matplotlib.pyplot as plt fig = plt.figure(figsize=(6, 4)) sub1 = fig.add_subplot(221) # equivalent to: plt.subplot(2, 2, 1) sub1.set_xticks([]) sub1.set_yticks([]) sub1.text(0.5, # x coordinate, 0 leftmost positioned, 1 rightmost0.5, # y coordinate, 0 topmost positioned, 1 bottommost'subplot(2,2,1)', # the text which will be printedhorizontalalignment='center', # shortcut 'ha' verticalalignment='center', # shortcut 'va'fontsize=20, # can be named 'font' as wellalpha=.5 # float (0.0 transparent through 1.0 opaque)) sub2 = fig.add_subplot(224, facecolor=python_course_green) sub2.set_xticks([]) sub2.set_yticks([]) python_course_green = "#90EE90" sub2.text(0.5, 0.5, 'subplot(2,2,4)', ha='center', va='center',fontsize=20, color="b") plt.show()
前面的示例僅顯示如何創建subplot設計。 下面我們演示如何使用一些真實的圖填充先前的子圖設計:
另外一個例子:
import matplotlib.pyplot as plt X = [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ] for nrows, ncols, plot_number in X:plt.subplot(nrows, ncols, plot_number) plt.show()以下示例沒有任何特殊之處。 我們將去除xticks并改變figure和subplots的大小。 為此,引入figure的關鍵字參數figsize和函數subplot_adjust及其關鍵字參數bottom, left,top, right:
import matplotlib.pyplot as plt fig =plt.figure(figsize=(6,4)) fig.subplots_adjust(bottom=0.025, left=0.025, top = 0.975, right=0.975) X = [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ] for nrows, ncols, plot_number in X:sub = fig.add_subplot(nrows, ncols, plot_number)sub.set_xticks([])sub.set_yticks([]) plt.show()15.2.替代解決方案:
由于需要合并2x3網格的前三個子圖,因此可以選擇元組表示,在(2,3,(1,3))中(1,3) 含義為一個2x3網格的前三個元素進行了合并:
import matplotlib.pyplot as plt fig =plt.figure(figsize=(6,4)) fig.subplots_adjust(bottom=0.025, left=0.025, top = 0.975, right=0.975) X = [ (2,3,(1,3)), (2,3,4), (2,3,5), (2,3,6) ] for nrows, ncols, plot_number in X:sub = fig.add_subplot(nrows, ncols, plot_number)sub.set_xticks([])sub.set_yticks([]) plt.show() 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的15_多子图-Subplot、Subplot: 使用多个figures和 axes、替代解决方案:的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 14_面向对象API绘图、图中图 (A
- 下一篇: 16、17、18_使用gridspec定