This is the script:
x=[0:0.1:10]; x1=5;
plot(x,2*sin(x),x,5*sin(x),x,cos(x));
line([x1 x1],???????);
I would like to plot the vertical line from the top to the bottom without knowing the y-axis limits.
Thanks in advance!

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 20 日

8 投票

x=[0:0.1:10];
x1=5;
plot(x,2*sin(x),x,5*sin(x),x,cos(x));
y1=get(gca,'ylim')
hold on
plot([x1 x1],y1)

2 件のコメント

Brando Miranda
Brando Miranda 2017 年 10 月 14 日
can u explain?
Eric Sargent
Eric Sargent 2020 年 6 月 23 日
Starting in R2018b you can use xline and yline.

サインインしてコメントする。

その他の回答 (5 件)

Kelly Kearney
Kelly Kearney 2014 年 10 月 20 日

4 投票

line([x1 x1], get(gca, 'ylim'));
Or this function will automate the process, if you have a lot of reference lines to plot.

5 件のコメント

Brando Miranda
Brando Miranda 2017 年 10 月 14 日
why does this work?
Image Analyst
Image Analyst 2017 年 10 月 14 日
get(gca, 'ylim') gives you a 2 element array with the lower limit on the y axis, and the upper limit on the y axis, whatever they are. The second argument of line is a list of the y values corresponding the x elements, so it will make a line from (x1, lower y limit) to x1, upper y limit).
Alberto Sivera
Alberto Sivera 2021 年 11 月 16 日
thx a lot, you saved my life, really
Image Analyst
Image Analyst 2021 年 11 月 17 日
@Alberto Sivera if you have R2018b or later, you should really be using xline() like @Pierre Tallotte shows in his answer below.
xline(x1, 'Color', 'r', 'LineWidth', 2);
Alberto Sivera
Alberto Sivera 2021 年 11 月 17 日
thank you, but unfortunately I have the r2017b version and i'm too lazy to update it :)

サインインしてコメントする。

Pierre Tallotte
Pierre Tallotte 2020 年 4 月 10 日

3 投票

The xline function is just what you need:
x=[0:0.1:10]; x1=5;
plot(x,2*sin(x),x,5*sin(x),x,cos(x));
xline(x1);
Image Analyst
Image Analyst 2014 年 10 月 20 日

1 投票

simply pass in ylim for the y array:
line([x1 x1], ylim);

3 件のコメント

Liyuan
Liyuan 2017 年 7 月 27 日
編集済み: Liyuan 2017 年 7 月 27 日
I am having a strange problem. When I draw the line with ylim the y axis automatically resizes such that my line doesn't extend fully to the top. Then when I draw another line with ylim it doesn't do this strange resizing. What could be the problem?
My code:
clf
x=0:0.1:10;
y1 = normpdf(x,5,1.5);
y2 = normpdf(x,6,1.5);
hold on
plot(x,y1)
plot(x,y2)
line([5 5],ylim)
Image Analyst
Image Analyst 2017 年 7 月 27 日
Not sure.
ylim() is not returning the correct y axis range limits.
That certainly is weird. I'd call tech support on this one.
Jan
Jan 2017 年 8 月 28 日
@Liyuan: See my answer.

サインインしてコメントする。

Jan
Jan 2017 年 8 月 28 日

1 投票

If the axes is scaled, e.g. when adding new objects or for printing, using the current limits for the Y-position is fragile. You can use much larger positions and exclude the line from the list of objects, which influence the auto-scaling:
YL = get(gca, 'ylim');
YR = YL(2) - YL(1);
YL = [YL(1) - 1000 * YR, YL(2) + 1000 * YR];
line([5, 5], YL, 'YLimInclude', 'off')
'YLimInclude' in undocumented and might be removed in the future.
Jefferson Martinez Saavedra
Jefferson Martinez Saavedra 2020 年 10 月 23 日

0 投票

Does someone know how to get/download xline and yline functions? I have R2018a's version of MATLAB.
Thank you in advance.

1 件のコメント

Image Analyst
Image Analyst 2020 年 10 月 23 日
If you dont' have xline and yline you can use line():
xl = xlim;
yl = ylim;
line([x, x], yl); % Old way of doing xline().
line(xl, [y, y]); % Old way of doing yline().

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by