Main Content

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

プロットでの色とライン スタイルの自動選択の制御

同じ座標軸に複数のデータセットを共にプロットすると、MATLAB® は外観を変更することでデータ セットを識別できるようにします。たとえば、複数のラインをプロットすると、MATLAB は作成順序に従って色のセットからの色を各ラインに割り当てます。色の数より多くの行を作成すると、MATLAB は最初の色から色のサイクルを繰り返します。この動作は以下のように変更できます。

このトピックでは、ライン プロットおよび散布図を構成する方法を説明しますが、色 (さらに場合によってはライン スタイル) のサイクル制御の同じ概念が、barareastem プロットなどの他の多くのプロットにも適用されます。このトピック内のすべての例では、プロットの "後に" 座標軸プロパティを設定しています。手順でこの順序に従うのが重要なのは、ほとんどのプロット関数で座標軸プロパティの多くがリセットされるためです。

メモ

プロットで特定の項目に対して色またはライン スタイルを指定する場合は、プロットの色の指定およびプロットのラインとマーカーの外観の指定を参照してください。

色とライン スタイルの異なるセットの指定

MATLAB で選択される色は、座標軸の ColorOrder プロパティから取得されます。このプロパティには、RGB 3 成分として指定された色からなる 3 列の行列が含まれます。RGB 3 成分は、色の赤、緑、青成分の強度を含む 3 要素ベクトルです。強度値は [0, 1] の範囲でなければなりません。

複数のラインをプロットした場合は、1 番目のラインには ColorOrder 行列の 1 番目の色を使用し、2 番目のラインには 2 番目の色を使用します。それ以降も同様です。行列の行数よりも多くの行がプロットに含まれている場合は、最終的に色は繰り返されます。このコードでは、既定の色の順序を使用する複数のライン プロットを作成します。最初の行は一番上のラインです。既定の ColorOrder 行列には 7 行が含まれているため、8 番目のラインから色が繰り返されます。

plot([9 10 11 12])
hold on
plot([8 9 10 11])
plot([7 8 9 10])
plot([6 7 8 9])
plot([5 6 7 8])
plot([4 5 6 7])
plot([3 4 5 6])
plot([2 3 4 5])
plot([1 2 3 4])
hold off
legend("Location","northeastoutside")

Plot containing nine solid lines with a legend. The first seven lines have unique colors. The last two lines use the same colors as the first two lines.

次の 2 つの方法のいずれかで色を変更できます。

  • 座標軸の ColorOrder プロパティを新しい RGB 3 成分の行列に設定する。

  • 関数 colororder を呼び出す。この関数は、RGB 3 成分、色名 ("red" など)、および 16 進数カラー コードを受け入れます。 (R2019b 以降) また、複数用意されている事前定義されたパレット名 ("gem""reef""meadow" など) のいずれかも受け入れます。 (R2023b 以降)

赤、緑、および青の RGB 3 成分を含む新しい行列を作成します。次に、ColorOrder プロパティをその行列に設定します。プロットが即時に更新され、新しい色が反映されます。

mycolors = [1 0 0; 0 1 0; 0 0 1];
ax = gca; 
ax.ColorOrder = mycolors;

Plot containing nine solid lines with a legend. The first three lines have unique colors. The colors are repeated two more times for the last six lines.

MATLAB では、色に加えて異なるライン スタイルも順番に使用します。既定では、ライン スタイルは 1 つのみです (実線)。追加のライン スタイルを指定するには、座標軸の LineStyleOrder プロパティを設定します。たとえば、以下のコードでは 3 つのライン スタイルを指定しています。更新されたプロットでは、1 つのライン スタイルですべての色を 1 巡してから、次のライン スタイルを表示します。

mylinestyles = ["-"; "--"; "-o"];
ax.LineStyleOrder = mylinestyles;

Plot containing three solid lines, three dashed lines, and three solid lines with circular markers. The three colors are repeated for each set of three lines. The plot also has a legend.

色とライン スタイルのサイクル順の指定

R2023a 以降

複数の色とライン スタイルを使用する場合は、LineStyleCyclingMethod プロパティを設定することで、プロットですべてのライン スタイルを使用してから次の色を使用するのか、すべての色を使用してから次のライン スタイルを使用するのか、色と同時にライン スタイルを順に変えて使用するのかを指定できます。このプロパティには以下の 3 つの値のいずれかを設定できます。

  • "aftercolor" — すべての色を順に使用した後に次のライン スタイルを使用します。この値が既定値です。

  • "beforecolor" — すべてのライン スタイルを順に使用した後に次の色を使用します。

  • "withcolor" — 色を同時に変えながら、ライン スタイルを順番に使用します。

4 本のラインをプロットします。関数 colororder に 3 つの 16 進数カラー コードの配列を渡して、LineStyleOrder プロパティを 3 つのライン スタイルに設定し、ColorOrder プロパティを 3 つの色に設定します。次に、凡例を追加します。

% Plot four lines
plot([4 5 6 7])
hold on
plot([3 4 5 6])
plot([2 3 4 5])
plot([1 2 3 4])
hold off

% Set the line style order and color order
ax = gca;
ax.LineStyleOrder = ["-"; "--"; "-o"];
colororder(["#8040E6";"#1AA640";"#E68000"])
legend("Location","northeastoutside")

Plot containing four lines with three colors and two line styles. The first three lines are solid with unique colors. The last line is dashed and the has the same color as the first solid line. The plot also has a legend.

既定では、プロットは最初のライン スタイル (実線) ですべての色を順に使用してから、次のライン スタイル (破線) を表示します。3 色であるため、4 つのラインのプロットではライン スタイルは 2 つだけ使用されます。

さらに多くのラインを色とライン スタイルの両方で区別できるようにするには、"withcolor" オプションを使用し、同じ数の色とライン スタイルを指定します。たとえば、上記のプロットの LineStyleCylingMethod"withcolor" に変更します。更新されたプロットでは、色とライン スタイルを同時に順に変えて使用します。

ax.LineStyleCyclingMethod = "withcolor";

Plot containing four lines with three unique colors and three unique line styles. The last line has the same color and line style as the first line. The plot also has a legend.

色またはライン スタイルでのデータのグループ化

R2020a 以降

関連するライン (または他のプロット オブジェクト) を視覚的にグループ化するには、同じ視覚的処理を適用する各オブジェクトの SeriesIndex プロパティを同じ数値に設定します。SeriesIndex プロパティは、LineStyleCyclingMethod プロパティの値に従って、配列 ColorOrder および LineStyleOrder へのインデックス付けを行います。これにより、同じ SeriesIndex 値をもつ異なるオブジェクトで同じ色 (および、該当する場合は同じライン スタイル) が使用されます。

たとえば、50 個の散布点の 2 つのセットをプロットします。

x = 1:50;
meas1 = 0.25*x + randn(1,50);
scat1 = scatter(x,meas1);
hold on
meas2 = 0.5*x + randn(1,50) + 5;
scat2 = scatter(x,meas2);

Two sets of scattered points that are offset from each other vertically. The top set of points is red, and the bottom set of points is blue.

関数 polyfit および polyval を使用して点の各セットの近似線を計算します。次に、各近似線をプロットに追加します。

% Calculate fit lines for each set of measurements
p1 = polyfit(x,meas1,1);
y1_fit = polyval(p1,x);
p2 = polyfit(x,meas2,1);
y2_fit = polyval(p2,x);

% Plot fit lines
fitline1 = plot(x,y1_fit);
fitline2 = plot(x,y2_fit);
hold off

Plot of two sets of scattered points with fit lines that run through them. Each set of points and corresponding fit line are vertically displaced from the other set of points and fit line. The top set of points is red and the fit line is purple. The bottom set of points is blue and the fit line is yellow.

散布図および近似線はすべて異なる色になります。これは、座標軸に追加した新しいプロットごとに MATLAB で ColorOrder 行列から新しい色が選択されるためです。

各近似線の色を関連付けられている散布図に合わせます。各ラインの SeriesIndex プロパティを、関連付けられている散布図と同じ値に設定します。

fitline1.SeriesIndex = scat1.SeriesIndex;
fitline2.SeriesIndex = scat2.SeriesIndex;

Plot of two sets of scattered points with fit lines that run through them. Each set of points and corresponding fit line are vertically displaced from the other set of points and fit line. The top set of points and fit line are both red. The bottom set of points and fit line are both blue.

この関係は、ColorOrder 行列の色を変更しても維持されます。たとえば、関数 colororder を呼び出して色を紫色と緑色に変更します。

colororder([0.5 0.25 0.90; 0.10 0.65 0.25])

Plot of two sets of scattered points with fit lines that run through them. Each set of points and corresponding fit line are vertically displaced from the other set of points and fit line. The top set of points and fit line are both green. The bottom set of points and fit line are both purple.

参考

関数

プロパティ

関連するトピック