Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

2 次元および 3 次元プロット

ライン プロット

2 次元ライン プロットを作成するには、関数 plot を使用します。たとえば、0 ~ 2π の範囲で線形に配置された値のベクトルで正弦関数をプロットします。

x = linspace(0,2*pi);
y = sin(x);
plot(x,y)

Figure contains an axes object. The axes object contains an object of type line.

座標軸にラベルを付け、タイトルを加えることができます。

xlabel("x")
ylabel("sin(x)")
title("Plot of the Sine Function")

Figure contains an axes object. The axes object with title Plot of the Sine Function, xlabel x, ylabel sin(x) contains an object of type line.

関数 plot に 3 番目の入力引数を追加することにより、同じ変数を赤い破線でプロットできます。

plot(x,y,"r--")

Figure contains an axes object. The axes object contains an object of type line.

"r--""ライン仕様" です。それぞれの仕様には、ラインの色、スタイルおよびマーカーを表す文字を含めることができます。マーカーとは、プロットされた各データ点に表示される、+o* などの記号です。たとえば "g:*" では、緑色の点線に * がマーカーとして表示されます。

現在の Figure ウィンドウ内に最初のプロットで定義したタイトルとラベルが表示されなくなったことに注目してください。既定では、プロット関数を呼び出すたびに MATLAB® によって Figure がクリアされ、新しいプロットを準備するために座標軸や他の要素がリセットされます。

既存の Figure にプロットを追加するには、hold on を使用します。hold off を使用するかウィンドウを閉じるまで、すべてのプロットが現在の Figure ウィンドウに表示されます。

x = linspace(0,2*pi);
y = sin(x);
plot(x,y)

hold on

y2 = cos(x);
plot(x,y2,":")
legend("sin","cos")

hold off

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent sin, cos.

3 次元プロット

通常、3 次元プロットでは、z=f(x,y) のような 2 変数関数によって定義される表面が表示されます。たとえば、それぞれ [-2,2] の範囲の 20 個の点をもつ行ベクトルと列ベクトル xy について、z=xe-x2-y2 を計算します。

x = linspace(-2,2,20);
y = x';
z = x .* exp(-x.^2 - y.^2);

次に、表面のプロットを作成します。

surf(x,y,z)

Figure contains an axes object. The axes object contains an object of type surface.

関数 surf とその対になる関数 mesh により、表面が 3 次元で表示されます。surf は、接続線と表面の構成面をカラー表示します。mesh は、接続線のみをカラー表示した、ワイヤーフレームの表面を描きます。

複数のプロット

tiledlayout または subplot を使用して、同じウィンドウの異なる部分に複数のプロットを表示することができます。

関数 tiledlayout は R2019b で導入されたもので、ラベルと間隔について subplot よりも細かい制御を提供します。たとえば、Figure ウィンドウ内に 2 行 2 列のレイアウトを作成します。その後、次の領域にプロットを表示するたびに nexttile を呼び出します。

t = tiledlayout(2,2);
title(t,"Trigonometric Functions")
x = linspace(0,30);

nexttile
plot(x,sin(x))
title("Sine")

nexttile
plot(x,cos(x))
title("Cosine")

nexttile
plot(x,tan(x))
title("Tangent")

nexttile
plot(x,sec(x))
title("Secant")

Figure contains 4 axes objects. Axes object 1 with title Sine contains an object of type line. Axes object 2 with title Cosine contains an object of type line. Axes object 3 with title Tangent contains an object of type line. Axes object 4 with title Secant contains an object of type line.

R2019b よりも前のリリースを使用している場合は、subplotを参照してください。