Main Content

グラフへの凡例の追加

凡例は、グラフ上にプロットされたデータ系列にラベル付けを行うために便利な方法です。これらの例は、凡例を作成し、位置の変更、フォント サイズの設定、タイトルの追加などの一般的な変更を行うための方法を示します。さらに、複数列からなる凡例を作成したり、プロットされたデータのサブセットに対する凡例を作成することもできます。

単純な凡例の作成

線グラフと散布図からなる Figure を作成します。それぞれの図の説明を含む凡例を追加します。凡例ラベルを関数 legend への入力として指定します。

figure
x1 = linspace(0,5);
y1 = sin(x1/2);
plot(x1,y1)

hold on
x2 = [0 1 2 3 4 5];
y2 = [0.2 0.3 0.6 1 0.7 0.6];
scatter(x2,y2,'filled')
hold off

legend('sin(x/2)','2016')

DisplayName を使用したラベルの指定

別の方法として、DisplayName プロパティを使用して凡例のラベルを指定することができます。プロット関数を呼び出すときに、名前と値のペアとして DisplayName プロパティを設定します。次に、legend コマンドを呼び出して凡例を作成します。

x1 = linspace(0,5);
y1 = sin(x1/2);
plot(x1,y1,'DisplayName','sin(x/2)')

hold on
x2 = [0 1 2 3 4 5];
y2 = [0.2 0.3 0.6 1 0.7 0.6];
scatter(x2,y2,'filled','DisplayName','2016')

legend

データ系列を追加または削除すると、凡例が自動的に更新されます。座標軸にデータを追加する際は、DisplayName プロパティを使用してラベルを指定します。DisplayName プロパティを設定しない場合、凡例では 'dataN' 形式のラベルが使用されます。

2017 年のデータの散布図を追加します。

x3 = [0 1 2 3 4 5];
y3 = [0.1 0.4 0.6 0.9 0.8 0.7];
scatter(x3,y3,'filled','DisplayName','2017')
drawnow
hold off

凡例の外観のカスタマイズ

関数 legend により Legend オブジェクトが作成されます。Legend オブジェクトには、LocationOrientationFontSize、および Title プロパティなど、凡例の外観のカスタマイズに使用できるプロパティがあります。完全な一覧については、Legend のプロパティを参照してください。

次の 2 つの方法でプロパティを設定できます。

  • legend コマンドで名前と値のペアを使用。ほとんどの場合、名前と値のペアを使用する際は、legend({'label1','label2'},'FontSize',14) のように cell 配列でラベルを指定しなければなりません。

  • Legend オブジェクトを使用。Legend オブジェクトを、lgd = legend のように、関数 legend の出力引数として返すことができます。次に、lgd.FontSize = 14 のように、lgd をドット表記と共に使用してプロパティを設定します。

凡例の位置と向き

Location プロパティと Orientation プロパティを名前と値のペアとして設定することにより、凡例の位置と向きを指定します。8 つある基本方位と中間方位のいずれかに位置を設定します (この場合、'northwest')。向きを 'vertical' (既定) または 'horizontal' (この場合) に設定します。cell 配列でラベルを指定します。

x1 = linspace(0,5);
y1 = sin(x1/2);
plot(x1,y1)

hold on
x2 = [0 1 2 3 4 5];
y2 = [0.2 0.3 0.6 1 0.7 0.6];
scatter(x2,y2,'filled')
hold off

legend({'sin(x/2)','2016'},'Location','northwest','Orientation','horizontal')

凡例のフォント サイズとタイトル

FontSize プロパティと Title プロパティを設定することにより、凡例のフォント サイズとタイトルを指定します。Legend オブジェクトを変数 lgd に代入します。次に、ドット表記を使用して、lgd でプロパティを変更します。

x1 = linspace(0,5);
y1 = sin(x1/2);
plot(x1,y1,'DisplayName','sin(x/2)')

hold on
x2 = [0 1 2 3 4 5];
y2 = [0.2 0.3 0.6 1 0.7 0.6];
scatter(x2,y2,'filled','DisplayName','2016')
hold off

lgd = legend;
lgd.FontSize = 14;
lgd.Title.String = '2016 Data';

複数列からなる凡例

6 つのライン プロットからなるチャートを作成します。NumColumns プロパティを 2 に設定することにより、2 列からなる凡例を追加します。

x = linspace(0,10);
y1 = sin(x);
y2 = sin(0.9*x);
y3 = sin(0.8*x);
y4 = sin(0.7*x);
y5 = sin(0.6*x);
y6 = sin(0.5*x);

plot(x,y1,'DisplayName','sin(x)')
hold on
plot(x,y2,'DisplayName','sin(0.9x)')
plot(x,y3,'DisplayName','sin(0.8x)')
plot(x,y4,'DisplayName','sin(0.7x)')
plot(x,y5,'DisplayName','sin(0.6x)')
plot(x,y6,'DisplayName','sin(0.5x)')
hold off

lgd = legend;
lgd.NumColumns = 2;

チャートのサブセットを凡例に含める

2 つの棒グラフと 1 つの散布図を組み合わせます。Bar オブジェクト (b1 および b2) を、関数 legend の最初の入力引数として指定することで、棒グラフのみが含まれる凡例を作成します。オブジェクトをベクトルで指定します。

x = [1 2 3 4 5];
y1 = [.2 .4 .6 .4 .2];
b1 = bar(x,y1);

hold on 
y2 = [.1 .3 .5 .3 .1];
b2 = bar(x,y2,'BarWidth',0.5);

y3 = [.2 .4 .6 .4 .2];
s = scatter(x,y3,'filled');
hold off

legend([b1 b2],'Bar Chart 1','Bar Chart 2')

参考

|