Main Content

subplot

タイル状に配置された Axes の作成

説明

メモ

tiledlayout は、調整可能なタイルの間隔、Figure のサイズに応じてリフローするタイル、より適切に配置されたカラーバーと凡例を使用してレイアウトを作成できるため、subplot よりも推奨されます。 (R2019b 以降)

subplot(m,n,p) は現在の Figure を mn 列のグリッドに分割し、p で指定された位置に座標軸を作成します。MATLAB® は行ごとにサブプロットの位置に番号を付けます。1 番目のサブプロットは 1 行目の 1 列目、2 番目のサブプロットは 1 行目の 2 列目となり、以下同様に続きます。指定した位置に座標軸がある場合、このコマンドはその座標軸を現在の座標軸にします。

subplot(m,n,p,'replace') は位置 p にある既存の座標軸を削除し、新しい座標軸を作成します。

subplot(m,n,p,'align') は、プロット ボックスが整列するように新しい座標軸を作成します。このオプションは既定の動作です。

subplot(m,n,p,ax) は、既存の座標軸 ax を同じ Figure のサブプロットに変換します。

subplot('Position',pos) は、pos で指定したカスタム位置に座標軸を作成します。このオプションを使用すると、グリッド位置に整列していないサブプロットを配置できます。pos は、[left bottom width height] の形式の 4 要素ベクトルとして指定します。新しい座標軸が既存の座標軸とオーバーラップする場合は、新しい座標軸が既存の座標軸を置き換えます。

subplot(___,Name,Value) は、1 つ以上の名前と値のペア引数を使用して、座標軸のプロパティを変更します。座標軸のプロパティは他のすべての入力引数の後に設定します。

ax = subplot(___) は、Axes オブジェクト、PolarAxes オブジェクトまたは GeographicAxes オブジェクトを作成します。ax を使用すると座標軸を後から変更できます。

subplot(ax) は、ax で指定した座標軸を親 Figure の現在の座標軸にします。このオプションでは、親 Figure が既に現在の Figure になっていない場合、親 Figure が現在の Figure になることはありません。

すべて折りたたむ

2 つのサブプロットを積み上げた Figure を作成します。それぞれに正弦波をプロットします。

subplot(2,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)

subplot(2,1,2); 
y2 = sin(5*x);
plot(x,y2)

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

4 つのサブプロットに分かれた 1 つの Figure を作成します。それぞれに正弦波をプロットし、各サブプロットにタイトルを付けます。

subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')

subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')

subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')

subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')

Figure contains 4 axes objects. Axes object 1 with title Subplot 1: sin(x) contains an object of type line. Axes object 2 with title Subplot 2: sin(2x) contains an object of type line. Axes object 3 with title Subplot 3: sin(4x) contains an object of type line. Axes object 4 with title Subplot 4: sin(8x) contains an object of type line.

3 つのサブプロットがある Figure を作成します。Figure の上半分を占めるサブプロットを 2 つ作成し、Figure の下半分を占める 3 番目のサブプロットを作成します。各サブプロットにタイトルを追加します。

subplot(2,2,1);
x = linspace(-3.8,3.8);
y_cos = cos(x);
plot(x,y_cos);
title('Subplot 1: Cosine')

subplot(2,2,2);
y_poly = 1 - x.^2./2 + x.^4./24;
plot(x,y_poly,'g');
title('Subplot 2: Polynomial')

subplot(2,2,[3,4]);
plot(x,y_cos,'b',x,y_poly,'g');
title('Subplot 3 and 4: Both')

Figure contains 3 axes objects. Axes object 1 with title Subplot 1: Cosine contains an object of type line. Axes object 2 with title Subplot 2: Polynomial contains an object of type line. Axes object 3 with title Subplot 3 and 4: Both contains 2 objects of type line.

乱数データのステム プロットを 4 つ含む Figure を作成します。2 番目のサブプロットを空の座標軸に置き換えます。

for k = 1:4
    data = rand(1,10);
    subplot(2,2,k)
    stem(data)
end

Figure contains 4 axes objects. Axes object 1 contains an object of type stem. Axes object 2 contains an object of type stem. Axes object 3 contains an object of type stem. Axes object 4 contains an object of type stem.

subplot(2,2,2,'replace')

Figure contains 4 axes objects. Axes object 1 contains an object of type stem. Axes object 2 contains an object of type stem. Axes object 3 contains an object of type stem. Axes object 4 is empty.

グリッド位置に整列していない 2 つのサブプロットを含む Figure を作成します。サブプロットごとにカスタム位置を指定します。

pos1 = [0.1 0.3 0.3 0.3];
subplot('Position',pos1)
y = magic(4);
plot(y)
title('First Subplot')

pos2 = [0.5 0.15 0.4 0.7];
subplot('Position',pos2)
bar(y)
title('Second Subplot')

Figure contains 2 axes objects. Axes object 1 with title First Subplot contains 4 objects of type line. Axes object 2 with title Second Subplot contains 4 objects of type bar.

2 つの極座標軸をもつ Figure を作成します。上のサブプロットに極座標の線グラフを作成し、下のサブプロットに極座標の散布図を作成します。

figure
ax1 = subplot(2,1,1,polaraxes);
theta = linspace(0,2*pi,50);
rho = sin(theta).*cos(theta);
polarplot(ax1,theta,rho)

ax2 = subplot(2,1,2,polaraxes);
polarscatter(ax2,theta,rho)

Figure contains 2 axes objects. Polaraxes object 1 contains an object of type line. Polaraxes object 2 contains an object of type scatter.

2 つのサブプロットを含む Figure を作成します。Axes オブジェクトを変数 ax1 および変数 ax2 に代入します。Axes オブジェクトをプロット関数の入力として指定し、関数が特定のサブプロットに必ずプロットされるようにします。

ax1 = subplot(2,1,1);
Z = peaks;
plot(ax1,Z(1:20,:))

ax2 = subplot(2,1,2);  
plot(ax2,Z)

Figure contains 2 axes objects. Axes object 1 contains 49 objects of type line. Axes object 2 contains 49 objects of type line.

Axes オブジェクトのプロパティを設定して、座標軸を変更します。上のサブプロットのフォント サイズと、下のサブプロットの線幅を変更します。一部のプロット関数は、座標軸プロパティを設定します。既存の Axes プロパティの設定がオーバーライドされるのを防ぐために、プロット関数は Axes プロパティを指定する前に実行します。プロパティの設定にはドット表記を使用します。

ax1.FontSize = 15;
ax2.LineWidth = 2;

Figure contains 2 axes objects. Axes object 1 contains 49 objects of type line. Axes object 2 contains 49 objects of type line.

複数のサブプロットを含む Figure を作成します。Axes オブジェクトをベクトル ax に保存します。次に、2 番目のサブプロットを現在の座標軸にします。線グラフを作成し、2 番目のサブプロットの軸の範囲を変更します。既定では、グラフィックス関数は現在の座標軸がその対象となります。

for k = 1:4
    ax(k) = subplot(2,2,k);
end

subplot(ax(2))
x = linspace(1,50);
y = sin(x);
plot(x,y,'Color',[0.1, 0.5, 0.1])
title('Second Subplot')
axis([0 50 -1 1])

Figure contains 4 axes objects. Axes object 1 is empty. Axes object 2 with title Second Subplot contains an object of type line. Axes object 3 is empty. Axes object 4 is empty.

線グラフを作成します。次に、座標軸が Figure の下のサブプロットとなるように変換します。関数 subplot は、元の座標軸が存在していた Figure を使用します。

x = linspace(1,10);
y = sin(x);
plot(x,y)
title('Sine Plot')

Figure contains an axes object. The axes object with title Sine Plot contains an object of type line.

ax = gca;
subplot(2,1,2,ax)

Figure contains an axes object. The axes object with title Sine Plot contains an object of type line.

別々の Figure にある座標軸を組み合わせ、サブプロットをもつ単一の Figure にします。

2 つの異なる Figure に 2 つのプロットを作成します。Axes オブジェクトを変数 ax1 および変数 ax2 に代入します。Legend オブジェクトを変数 lgd に代入します。

figure
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Line Plot 1')
ax1 = gca;

Plot of a sine wave entitled "Line Plot 1"

figure
y2 = 2*sin(x);
plot(x,y2)
title('Line Plot 2')
lgd = legend('2*Sin(x)');
ax2 = gca;

Plot of a sine wave with a legend entitled "Line Plot 2"

copyobj を使用して 2 つの Axes オブジェクトのコピーを作成します。コピーした座標軸の親を新しい Figure として指定します。凡例とカラーバーは、関連する座標軸と一緒にはコピーされないため、座標軸と共に凡例をコピーします。

fnew = figure;
ax1_copy = copyobj(ax1,fnew);
subplot(2,1,1,ax1_copy)

copies = copyobj([ax2,lgd],fnew);
ax2_copy = copies(1);
subplot(2,1,2,ax2_copy)

Figure containing both of the preceding plots

入力引数

すべて折りたたむ

グリッド行の数。正の整数として指定します。

データ型: single | double

グリッド列の数。正の整数として指定します。

データ型: single | double

新しい Axes のグリッド位置。正の整数のスカラーまたはベクトルとして指定します。

  • p が正のスカラー整数の場合、subplot はグリッド位置 p にサブプロットを作成します。

  • p が正の整数のベクトルの場合、subplotp にリストされたグリッド位置にまたがるサブプロットを作成します。

例: subplot(2,3,1) は位置 1 にサブプロットを作成します。

例: subplot(2,3,[2,5]) は位置 2 および 5 にまたがるサブプロットを作成します。

例: subplot(2,3,[2,6]) は位置 2、3、5 および 6 にまたがるサブプロットを作成します。

データ型: single | double

新しい Axes のカスタム位置。[left bottom width height] という形式の 4 要素ベクトルとして指定します。

  • left 要素と bottom 要素は、Figure の左下隅に対するサブプロットの左下隅の位置を指定します。

  • width 要素と height 要素は、サブプロットの寸法を指定します。

Figure の内部に対して正規化された、01 の範囲の値を指定してください。

メモ

スクリプトを使用してサブプロットを作成する場合、MATLAB は、drawnow コマンドが実行されるか、MATLAB がユーザー コマンドの待機に戻るまで、Position プロパティ値を確定しません。サブプロットの Position プロパティ値は、スクリプトがプロットを更新するか終了するまで、変更される可能性があります。

例: subplot('Position',[0.1 0.1 0.45 0.45])

データ型: single | double

現在の座標軸にする対象、またはサブプロットに変換する対象である既存の座標軸。Axes オブジェクト、PolarAxes オブジェクト、GeographicAxes オブジェクト、または HeatmapChart オブジェクトなどの PositionConstraint プロパティをもつグラフィックス オブジェクトとして指定します。

空の極座標軸または地理座標軸をサブプロットの位置に作成するには、ax を関数 polaraxes または関数 geoaxes として指定します。たとえば、subplot(2,1,2,polaraxes) とします。

名前と値の引数

引数のオプションのペアを Name1=Value1,...,NameN=ValueN として指定します。ここで Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後になければなりませんが、ペアの順序は重要ではありません。

R2021a より前では、コンマを使用してそれぞれの名前と値を区切り、Name を引用符で囲みます。

例: subplot(m,n,p,'XGrid','on')

一部のプロット関数は、プロパティ設定をオーバーライドします。プロットの後に座標軸プロパティを設定することを検討してください。設定できるプロパティは座標軸のタイプによって異なります。

ヒント

  • Figure の内容をクリアするには、clf を使用します。たとえば、新しいサブプロット レイアウトを作成する前に、Figure から既存のサブプロット レイアウトをクリアできます。

  • 座標軸を重ねるには、代わりに axes コマンドを使用します。関数 subplot は、新しい座標軸とオーバーラップしている既存の座標軸を削除します。たとえば、subplot('Position',[.35 .35 .3 .3]) は基となる座標軸をすべて削除しますが、axes('Position',[.35 .35 .3 .3]) は基となる座標軸を削除せずに、Figure の中央に新しい座標軸を配置します。

  • subplot(111) は例外で、subplot(1,1,1) とは動作が同一ではありません。subplot(111) は下位互換性を確保するための特殊なサブプロットで、すぐには座標軸を作成せず、次のグラフィックス コマンドが clf reset を実行するように Figure を設定します。次のグラフィックス コマンドは Figure の子をすべて削除し、既定の位置に新しい座標軸を作成します。subplot(111)Axes オブジェクトを返さず、コードで戻り引数を指定するとエラーが発生します。

代替機能

設定可能なプロットのタイル配置を作成するには、関数 tiledlayout と関数 nexttile を使用します。次を含む設定オプションがあります。

  • プロット間およびレイアウトの縁周辺の間隔の制御

  • レイアウトの上部に共有タイトルを表示するためのオプション

  • x 軸と y 軸の共有ラベルを使用するためのオプション

  • タイル配置を固定サイズとするかリフロー可能な可変サイズとするかを制御するオプション

詳細については、複数のプロットの結合を参照してください。

バージョン履歴

R2006a より前に導入