Main Content

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

2 次元ライン プロットの作成

単純なライン プロットを作成し、座標軸にラベルを付けます。ラインの色とライン スタイルの変更、およびマーカーの追加により、プロットされる線の外観をカスタマイズします。

ライン プロットの作成

関数 plot を使用して 2 次元のライン プロットを作成します。たとえば、正弦関数の値を 0 ~ 2π の間でプロットするには、次のようにします。

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

座標軸にラベルを付け、タイトルを加えます。

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.

複数のラインのプロット

既定では、プロット コマンドごとに、事前に MATLAB によって Figure がクリアされます。新しい Figure ウィンドウを開くには、figure コマンドを使用します。hold on コマンドを使用することで、複数のラインをプロットできます。hold off を使用するかウィンドウを閉じるまで、すべてのプロットが現在の Figure ウィンドウに表示されます。

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

hold on 
y2 = cos(x);
plot(x,y2)
hold off

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

ラインの外観の変更

関数 plot を呼び出すときに、オプションのライン仕様を含めることによって、ラインの色やライン スタイルの変更、またはマーカーの追加を行うことができます。以下に例を示します。

  • ':' は点線をプロット。

  • 'g:' は緑色の点線をプロット。

  • 'g:*' は緑色の点線を星印のマーカーと共にプロット。

  • '*' はラインなしで星印のマーカーをプロット。

記号の順番は任意です。3 つの特性 (ラインの色、スタイル、マーカー) をすべて指定する必要はありません。さまざまなスタイル オプションの詳細については、関数 plot のページを参照してください。

たとえば、点線をプロットします。円形マーカー付きの赤い破線を使用する 2 番目のプロットを追加します。

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

hold on 
y2 = cos(x);
plot(x,y2,'--ro')
hold off

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

ライン仕様のライン スタイル オプションを省略し、データ点のみをプロットします。

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

Figure contains an axes object. The axes contains a line object which displays its values using only markers.

line オブジェクトのプロパティの変更

プロットの作成に使用する Line オブジェクトのプロパティを変更することによっても、プロットの外観をカスタマイズできます。

ライン プロットを作成します。作成した Line オブジェクトを変数 ln に代入します。ディスプレイには、ColorLineStyle、および LineWidth などのよく使用するプロパティが表示されます。

x = linspace(0,2*pi,25);
y = sin(x);
ln = plot(x,y)
ln = 
  Line with properties:

              Color: [0 0.4470 0.7410]
          LineStyle: '-'
          LineWidth: 0.5000
             Marker: 'none'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: [0 0.2618 0.5236 0.7854 1.0472 1.3090 1.5708 1.8326 2.0944 2.3562 2.6180 2.8798 3.1416 3.4034 3.6652 3.9270 4.1888 4.4506 4.7124 4.9742 5.2360 5.4978 5.7596 6.0214 6.2832]
              YData: [0 0.2588 0.5000 0.7071 0.8660 0.9659 1 0.9659 0.8660 0.7071 0.5000 0.2588 1.2246e-16 -0.2588 -0.5000 -0.7071 -0.8660 -0.9659 -1 -0.9659 -0.8660 -0.7071 -0.5000 -0.2588 -2.4493e-16]

  Use GET to show all properties

個々のプロパティにアクセスするには、ドット表記を使用します。たとえば、ラインの幅を 2 ポイントに変更し、ラインの色を RGB 3 成分の値で設定します (この場合は [0 0.5 0.5])。青の円形マーカーを追加します。

ln.LineWidth = 2;
ln.Color = [0 0.5 0.5];
ln.Marker = 'o';
ln.MarkerEdgeColor = 'b';

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

参考

| | |

関連するトピック