How to plot vertical lines?
21 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Azzi Abdelmalek
2014-10-20
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 个评论
更多回答(5 个)
Kelly Kearney
2014-10-20
line([x1 x1], get(gca, 'ylim'));
5 个评论
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
2021-11-17
thank you, but unfortunately I have the r2017b version and i'm too lazy to update it :)
Image Analyst
2014-10-20
simply pass in ylim for the y array:
line([x1 x1], ylim);
3 个评论
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
2017-8-28
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.
0 个评论
Jefferson Martinez Saavedra
2020-10-23
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
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().
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!