Main Content

yline

定数の y 値をもつ水平線

説明

yline(y) は、現在の座標軸における 1 つ以上の y 座標の位置に水平線を作成します。たとえば、yline(2)y=2 の位置にラインを作成します。

yline(y,LineSpec) はライン スタイル、ラインの色、またはその両方を指定します。たとえば、yline([12 20 33],'--b') は 3 本の青の破線を作成します。

yline(y,LineSpec,labels) はラインにラベルを追加します。

yline(___,Name,Value) は 1 つ以上の名前と値のペアの引数を使用して、定数直線のプロパティを指定します。プロパティの一覧については、ConstantLine のプロパティ を参照してください。名前と値のペアは他のすべての入力引数の後で指定します。

yline(ax,___) は、現在の座標軸ではなく、指定された座標軸にラインを作成します。現在の座標軸を決定するには、gca を使用します。

yl = yline(___)ConstantLine オブジェクトまたは ConstantLine オブジェクトの配列を返します。yl を使用して、ラインが作成された後にそのプロパティの変更やクエリを行います。

すべて折りたたむ

y = 2.5 の位置に水平線を作成します。

yline(2.5);

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

ラベル付きのラインを作成するには、ライン スタイルも指定しなければなりません。既定のライン スタイルは実線 '-' です。

fplot(@(x) log(x));
xlim([0 15]);
ylim([0 4]);
yline(3,'-','Threshold');

Figure contains an axes object. The axes object contains 2 objects of type functionline, constantline.

一連の xy のベクトルを定義してプロットします。その後、プロットされた最大値よりも上と最小値よりも下の y 軸に余白をパディングします。

x = 0:0.1:60;
y = 4.*cos(x)./(x+2);
plot(x,y)
ylim padded

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

y の最大値と最小値を求めます。それらの位置に 'Max' および 'Min' というラベル付きの水平線を作成します。

ymax = max(y);
ymin = min(y);
yline([ymax ymin],'--',{'Max','Min'})

Figure contains an axes object. The axes object contains 3 objects of type line, constantline.

y = 4 の位置に青い一点鎖線を作成します。

yline(4,'-.b');

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

ラベルと指定されたライン幅を使用して水平の破線を作成します。

x = -2:0.25:2;
[X,Y] = meshgrid(x);
Z = X.*exp(-X.^2-Y.^2);
contour(X,Y,Z,30)

yl = yline(0,'--','y = 0','LineWidth',3);

Figure contains an axes object. The axes object contains 2 objects of type contour, constantline.

ラベルの水平方向の配置とラインの色を変更します。

yl.LabelHorizontalAlignment = 'center';
yl.Color = [.80 0 .40];

Figure contains an axes object. The axes object contains 2 objects of type contour, constantline.

関数 tiledlayout を呼び出して、1 行 2 列のタイル表示チャート レイアウトを作成します。関数 nexttile を呼び出して、axes オブジェクト ax1 および ax2 を作成します。次に、各座標軸にプロットします。ax1 を関数 yline に引き渡すことで、水平線を最初のプロットに追加します。

tiledlayout(1,2)
ax1 = nexttile;
x = -pi/2:pi/60:pi/2;
y1 = tan(sin(x)) + cos(sin(x)); 
plot(ax1,x,y1)

ax2 = nexttile;
x = -pi/2:pi/60:pi/2;
y2 = tan(cos(x)) + exp(x);
plot(ax2,x,y2)

yline(ax1,1.0);

Figure contains 2 axes objects. Axes object 1 contains 2 objects of type line, constantline. Axes object 2 contains an object of type line.

入力引数

すべて折りたたむ

y 座標。数値、categorical 値、datetime 値、または duration 値を含むスカラーまたはベクトルとして指定します。y のデータ型は y 軸のデータ型と一致しなければなりません。

例: yline(10)y=10 の位置にラインを表示します。

例: yline([13 20 33])y=13y=20、および y=33 の位置にラインを表示します。

データ型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | categorical | datetime | duration

ラインのスタイルと色。文字と記号を含む文字ベクトルまたは string スカラーとして指定します。文字と記号の順番は任意です。ライン スタイル、ラインの色、またはその両方を指定できます。'o' などのマーカー記号は無視されます。

例: '--g' は緑色の破線です。

ライン スタイル説明結果として得られる線
"-"実線

Sample of solid line

"--"破線

Sample of dashed line

":"点線

Sample of dotted line

"-."一点鎖線

Sample of dash-dotted line, with alternating dashes and dots

色名省略名外観
'red''r'

Sample of the color red

'green''g'

Sample of the color green

'blue''b'

Sample of the color blue

'cyan' 'c'

Sample of the color cyan

'magenta''m'

Sample of the color magenta

'yellow''y'

Sample of the color yellow

'black''k'

Sample of the color black

'white''w'

Sample of the color white

ターゲット座標軸。Axes オブジェクトとして指定します。yline によって現在の座標軸以外の座標軸にプロットする場合は、この引数を使用します。

ライン ラベル。文字ベクトル、文字ベクトルの cell 配列、string 配列、または数値配列として指定します。複数行のラベルを作成するには、string 配列、または文字ベクトルの cell 配列を使用します。

1 本のラインのラベルを作成

1 行のテキストを表示するには、文字ベクトルまたは string スカラーを指定します。複数行のテキストを表示するには、文字ベクトルの cell 配列または string 配列を指定します。

表示labels の指定方法
1 行のテキスト文字ベクトルまたは string スカラーを指定します。

yline(12,'-','Sample')

A horizontal line in an axes with a label

複数行のテキスト文字ベクトルの cell 配列または string 配列を指定します。配列の各要素は、テキストの各行です。
yline(12,'-',{'Sample','x=12'})

A horizontal line in an axes with a label that has two lines of text

複数のラインのラベルを作成

すべてのラインの横に同じテキストを表示するには、文字ベクトルまたは string スカラーを指定します。ラインごとに異なるテキストを表示するには、文字ベクトルの cell 配列または string 配列を指定します。

表示labels の指定方法
1 つの共有のテキスト ラベル文字ベクトルまたは string スカラーを指定します。

yline([13 20 33],'-','Sample')

Three horizontal lines in an axes with matching labels

ラインごとに異なるラベル文字ベクトルの cell 配列または string 配列を指定します。配列の各要素は、それぞれのラインのラベルです。labels 配列の要素の数は x の長さと一致しなければなりません。
label = {'Sample 1','Sample 2','Sample 3'};
yline([13 20 33],'-',label)

Three horizontal lines in an axes with different labels

一部が複数行の異なるラベル文字ベクトルの cell 配列または string 配列を作成します。関数 sprintf を使用してテキストに改行文字 ('\n') を挿入します。
label = {'Sample 1',sprintf('Sample 2\nx=20'),'Sample 3'};
yline([13 20 33],'-',label)

Three horizontal lines in an axes with different labels. The label for the second line has two lines of text

名前と値の引数

引数のオプションのペアを Name1=Value1,...,NameN=ValueN として指定します。ここで Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後になければなりませんが、ペアの順序は重要ではありません。

R2021a より前では、コンマを使用してそれぞれの名前と値を区切り、Name を引用符で囲みます。

例: yline(3,':','Median Price','LabelHorizontalAlignment','center') は、ラベルの水平方向の配置を 'center' として指定します。

メモ

ここでは、プロパティの一部だけを紹介しています。完全な一覧については、ConstantLine のプロパティ を参照してください。

ラインの色。RGB 3 成分、16 進数カラー コード、色名、または省略名として指定します。

カスタム色を使用する場合は、RGB 3 成分または 16 進数カラー コードを指定します。

  • RGB 3 成分は、色の赤、緑、青成分の強度を指定する 3 成分の行ベクトルです。強度値は [0,1] の範囲でなければなりません。たとえば [0.4 0.6 0.7] のようになります。

  • 16 進数カラー コードは、ハッシュ記号 (#) で始まり、3 桁または 6 桁の 0 から F までの範囲の 16 進数が続く string スカラーまたは文字ベクトルです。この値は大文字と小文字を区別しません。したがって、カラー コード "#FF8800""#ff8800""#F80"、および "#f80" は等価です。

あるいは、名前を使用して一部の一般的な色を指定できます。次の表に、名前の付いた色オプション、等価の RGB 3 成分、および 16 進数カラー コードを示します。

色名省略名RGB 3 成分16 進数カラー コード外観
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

"none"該当なし該当なし該当なし色なし

MATLAB® の多くのタイプのプロットで使用されている既定の色の RGB 3 成分および 16 進数カラー コードを次に示します。

RGB 3 成分16 進数カラー コード外観
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

例: 'g'

例: [0.6 0.2 0.5]

例: '#D2F9A7'

ライン幅。ポイント単位の正の値として指定します。

凡例ラベル。文字ベクトルまたは string スカラーとして指定します。legend コマンドを呼び出すまで、凡例は表示されません。テキストを指定しない場合、legend'dataN' 形式を使用してラベルを設定します。

ラインに対するラベルの水平方向の配置。次の表のいずれかのオプションとして指定します。

オプション説明
'right'ラインの右側

Horizontal line with the label on the right side

'left'ラインの左側

Horizontal line with the label on the left side

'center'ラインの中心

Horizontal line with the label horizontally centered

ラインに対するラベルの垂直方向の配置。次の表のいずれかのオプションとして指定します。

オプション説明
'top'ラインの上方。

Horizontal line with the label above the line

'middle'ラインの中央。ラベルによってラインは分割されます。

Horizontal line with the label centered vertically with the line

'bottom'ラインの下方。

Horizontal line with the label below the line

ラベルの向き。'aligned' または 'horizontal' として指定します。例を表に示します。

向き説明
'aligned'ラベルの向きは指定されたラインの向きと同じです。

Horizontal line with a horizontal label

'horizontal'ラインの向きに関係なく、ラベルは水平です。

Horizontal line with a horizontal label

アルゴリズム

座標軸の 3 次元表示で、定数直線は z 軸の範囲の中点に位置する x-y 平面に表示されます。座標軸が回転すると、それに従って定数直線も回転します。

バージョン履歴

R2018b で導入