How to draw line vertical to Y-axis?
이전 댓글 표시
Hey Matlab users,
If a = [1 2 3 4 5 6 7]; and b = [1 4 7 10 7 4 1]; and the plot it by : figure,plot(a,b), how can I draw let's say a red line vertical to the value 4 and 7 in the y-axis (parallel to x-axis)? So that i can see the window between 4 and 7.
Thanks so much in advance,
Mehdi
채택된 답변
추가 답변 (5개)
Noushin Farnoud
2011년 11월 15일
4 개 추천
If you want to draw a line that spans the entire xlim or ylim of your figure, then I suggest that you use hline or vline functions from: http://www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline
You can also plot multiple vertical or horizontal lines at the same time using these functions. For example: hline([5,6,8]) draws 3 horizontal lines at x=5, x=6 and x=8.
Eric Sargent
2019년 10월 15일
Sven
2011년 11월 2일
Hi Mehdi, try this after plotting:
xlims = get(gca,'XLim');
hold on
plot(xlims, [4 4], '-r')
plot(xlims, [7 7], '-r')
댓글 수: 4
Walter Roberson
2011년 11월 2일
Those would be horizontal lines, not vertical lines...
Sven
2011년 11월 2일
The questions is a little ambiguous. I went with vertical lines first however note that the questioner wants lines "parallel to x-axis".
Walter Roberson
2011년 11월 2일
Confusing...
Sven
2011년 11월 2일
Doh! I voted yours up and now it's above mine... that's a blow to my chances for getting accepted! heheh
James
2014년 3월 28일
There is an excellent answer over on http://stackoverflow.com/a/8108766/1194420 repeated below for convenience. ---
There exist an undocumented function graph2d.constantline:
plot(-2:5, (-2:5).^2-1)
%# vertical line
hx = graph2d.constantline(0, 'LineStyle',':', 'Color',[.7 .7 .7]);
changedependvar(hx,'x');
%# horizontal line
hy = graph2d.constantline(0, 'Color',[.7 .7 .7]);
changedependvar(hy,'y');
Wayne King
2011년 11월 2일
a = [1 2 3 4 5 6 7];
b = [1 4 7 10 7 4 1];
plot(a,b)
hold on
plot(a,4*ones(length(b)),'r');
plot(a,7*ones(length(b)),'r');
댓글 수: 1
Walter Roberson
2011년 11월 2일
A bit murky if "a" does not happen to be in sorted order, especially if the drawingmode is set to xor
카테고리
도움말 센터 및 File Exchange에서 Language Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!