日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

15_多子图-Subplot、Subplot: 使用多个figures和 axes、替代解决方案:

發(fā)布時(shí)間:2024/9/27 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 15_多子图-Subplot、Subplot: 使用多个figures和 axes、替代解决方案: 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

15.多子圖-Subplot
15.1.Subplot: 使用多個(gè)figures和 axes
15.2.替代解決方案:

15.多子圖-Subplot

Matplotlib繪圖時(shí)一個(gè)常見(jiàn)問(wèn)題是如何在一個(gè)圖中包含多個(gè)圖。即如何實(shí)現(xiàn)在一個(gè)窗口中有多個(gè)圖形,每個(gè)圖形都出現(xiàn)在子圖中。
我們將使用兩種不同的方法來(lái)實(shí)現(xiàn)這一目標(biāo):
?subplot
?gridspec

15.1.Subplot: 使用多個(gè)figures和 axes

subplot及其參數(shù):
subplot(nrows, ncols, plot_number)
如果將subplot應(yīng)用于一個(gè)figure,則該figure將在概念上分為nrows * ncols個(gè)子軸。
參數(shù)plot_number標(biāo)識(shí)函數(shù)調(diào)用創(chuàng)建的subplot。 plot_number的范圍可以從1到最大nrows * ncols。
如果三個(gè)參數(shù)的值小于10,則可以使用一個(gè)int參數(shù)調(diào)用函數(shù)subplot,其中百位數(shù)表示nrows,十位數(shù)表示ncols,個(gè)位數(shù)表示plot_number。 這意味著:可以寫(xiě)subplot(234)來(lái)代替subplot(2, 3, 4) 。

在下面的示例中,我們實(shí)現(xiàn)一個(gè)2x2網(wǎng)格的兩個(gè)子圖:

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個(gè)個(gè)位置)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個(gè)位置 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()

如果不需要在軸上設(shè)置刻度,可以將它們?cè)O(shè)置為空元組,并添加以下代碼行:

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類(lèi)的實(shí)例即使用面向?qū)ο蟮姆椒ā?br /> 下面重寫(xiě)前面的例子來(lái)展示。 在這種情況下,將add_subplot方法應(yīng)用于Figure對(duì)象。

import matplotlib.pyplot as plt fig = plt.figure(figsize=(6, 4)) sub1 = fig.add_subplot(221) # equivalent to: plt.subplot(2, 2, 1) 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=.7 # float (0.0 transparent through 1.0 opaque)) python_course_green = "#90EE90" sub2 = fig.add_subplot(224, facecolor=python_course_green) sub2.text(0.5, 0.5,'subplot(2,2,4)',ha='center', va='center',fontsize=20,color="b") plt.show()

讓我們?cè)俅稳コ潭取?這次不能使用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()


前面的示例僅顯示如何創(chuàng)建subplot設(shè)計(jì)。 下面我們演示如何使用一些真實(shí)的圖填充先前的子圖設(shè)計(jì):

import numpy as np from numpy import e, pi, sin, exp, cos import matplotlib.pyplot as plt def f(t):return exp(-t) * cos(2*pi*t) def fp(t):return -2*pi * exp(-t) * sin(2*pi*t) - e**(-t)*cos(2*pi*t) def g(t):return sin(t) * cos(1/(t+0.1)) def g(t):return sin(t) * cos(1/(t)) fig = plt.figure(figsize=(6, 4)) t = np.arange(-5.0, 1.0, 0.1) sub1 = fig.add_subplot(221) # instead of plt.subplot(2, 2, 1) sub1.set_title('The function f') # non OOP: plt.title('The function f') sub1.plot(t, f(t)) sub2 = fig.add_subplot(222, facecolor="lightgrey") sub2.set_title('fp, the derivation of f') sub2.plot(t, fp(t)) t = np.arange(-3.0, 2.0, 0.02) sub3 = fig.add_subplot(223) sub3.set_title('The function g') sub3.plot(t, g(t)) t = np.arange(-0.2, 0.2, 0.001) sub4 = fig.add_subplot(224, facecolor="lightgrey") sub4.set_title('A closer look at g') sub4.set_xticks([-0.2, -0.1, 0, 0.1, 0.2]) sub4.set_yticks([-0.15, -0.1, 0, 0.1, 0.15]) sub4.plot(t, g(t)) plt.plot(t, g(t)) plt.tight_layout() plt.show()

另外一個(gè)例子:

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()

以下示例沒(méi)有任何特殊之處。 我們將去除xticks并改變figure和subplots的大小。 為此,引入figure的關(guān)鍵字參數(shù)figsize和函數(shù)subplot_adjust及其關(guān)鍵字參數(shù)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網(wǎng)格的前三個(gè)子圖,因此可以選擇元組表示,在(2,3,(1,3))中(1,3) 含義為一個(gè)2x3網(wǎng)格的前三個(gè)元素進(jìn)行了合并:

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位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的15_多子图-Subplot、Subplot: 使用多个figures和 axes、替代解决方案:的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。