Main Content

チャートへのタイトルと軸のラベルの追加

この例では、関数 titlexlabel、および ylabel を使用して、タイトルと軸のラベルをチャートに追加する方法を示します。また、フォント サイズを変更して、座標軸のテキストの外観をカスタマイズする方法も示します。

簡単なライン プロットの作成

x-2π から 2π の間で線形に等間隔な 100 個の値として作成します。y1 および y2x の正弦値および余弦値として作成します。両方のデータセットをプロットします。

x = linspace(-2*pi,2*pi,100);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)

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

タイトルの追加

チャートにタイトルを追加するには、関数 title を使用します。ギリシャ文字 π を表示するために、TeX マークアップ \pi を使用します。

title('Line Plot of Sine and Cosine Between -2\pi and 2\pi')

Figure contains an axes object. The axes object with title Line Plot of Sine and Cosine Between - 2 pi blank a n d blank 2 pi contains 2 objects of type line.

軸ラベルの追加

チャートに軸ラベルを追加するには、関数 xlabel および関数 ylabel を使用します。

xlabel('-2\pi < x < 2\pi') 
ylabel('Sine and Cosine Values') 

Figure contains an axes object. The axes object with title Line Plot of Sine and Cosine Between - 2 pi blank a n d blank 2 pi, xlabel - 2 pi blank < blank x blank < blank 2 pi, ylabel Sine and Cosine Values contains 2 objects of type line.

凡例の追加

各データセットを識別する凡例をグラフに追加するには、関数 legend を使用します。ラインをプロットする順序で、凡例の説明を指定します。必要に応じて、8 つある基本方位と中間方位のいずれか (この場合は 'southwest') を使用して凡例の場所を指定します。

legend({'y = sin(x)','y = cos(x)'},'Location','southwest')

Figure contains an axes object. The axes object with title Line Plot of Sine and Cosine Between - 2 pi blank a n d blank 2 pi, xlabel - 2 pi blank < blank x blank < blank 2 pi, ylabel Sine and Cosine Values contains 2 objects of type line. These objects represent y = sin(x), y = cos(x).

フォント サイズの変更

Axes オブジェクトには、座標軸の外観のカスタマイズに使用できるプロパティがあります。たとえば、FontSize プロパティは、タイトル、ラベル、および凡例のフォントサイズを制御します。

関数 gca を使用して、現在の Axes オブジェクトにアクセスします。次に、ドット表記を使用して FontSize プロパティを設定します。

ax = gca;
ax.FontSize = 13;

Figure contains an axes object. The axes object with title Line Plot of Sine and Cosine Between - 2 pi blank a n d blank 2 pi, xlabel - 2 pi blank < blank x blank < blank 2 pi, ylabel Sine and Cosine Values contains 2 objects of type line. These objects represent y = sin(x), y = cos(x).

あるいは、R2022a 以降、関数fontsizeを使用して、座標軸テキストのフォント サイズを変更できます。

変数の値をもつタイトル

タイトル テキストに変数の値を含めるには、関数 num2str を使用して値をテキストに変換します。同様の方法を使用することで、軸ラベルまたは凡例項目に変数値を追加できます。

sin(π)/2 の値をもつタイトルを追加します。

k = sin(pi/2);
title(['sin(\pi/2) = ' num2str(k)])

Figure contains an axes object. The axes object with title s i n ( pi / 2 ) blank = blank 1, xlabel - 2 pi blank < blank x blank < blank 2 pi, ylabel Sine and Cosine Values contains 2 objects of type line. These objects represent y = sin(x), y = cos(x).

参考

| | | | |

関連するトピック