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

 採用された回答

Walter Roberson
Walter Roberson 2011 年 11 月 2 日

7 投票

The same as for your previous question. The solution for that was
yL = get(gca,'YLim');
line([3 3],yL,'Color','r');
line([5 5],yL,'Color','r');
and if you are wanting to draw vertical lines at 4 and 7, you would just change the code to
yL = get(gca,'YLim');
line([4 4],yL,'Color','r');
line([7 7],yL,'Color','r');
Perhaps what you want is instead horizontal lines? If so then
xL = get(gca,'XLim');
line(xL,[4 4],'Color','r');
line(xL,[7 7],'Color','r');

7 件のコメント

Sven
Sven 2011 年 11 月 2 日
Tiny little typo there Walter ;)
But you get my vote anyway for completeness :)
Mehdi, please note that your question title said "vertical" lines, but as Walter has pointed out, you were actually asking for "horizontal" lines
M G
M G 2011 年 11 月 2 日
Yes that's right, I meant horizontal line. Thanks Walter and Sven.
Walter Roberson
Walter Roberson 2011 年 11 月 2 日
Typo? Now you've got me going over it character by character, because I can't find a typo... agggh!
Sven
Sven 2011 年 11 月 6 日
Last line, Walter :)
Sven
Sven 2011 年 11 月 7 日
Ah... so now you go ahead and silently fix the typo just to make me look silly :)
Gourav Mitra
Gourav Mitra 2021 年 4 月 12 日
it works fine but in some case, the vertical line doesnot fit or cover the entire ylimit.
Walter Roberson
Walter Roberson 2021 年 4 月 12 日
These days, see yline()
If you get the current ylim and use it to draw, and then you zoom or pan, then the code I posted back then will not adjust to draw according to the new limits. However, the newer yline() should do that.

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

その他の回答 (5 件)

Noushin Farnoud
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
Eric Sargent 2019 年 10 月 15 日

3 投票

Starting in R2018b you can simply use xline and yline.
In this case:
yline(4,'r');
yline(7,'r');
Sven
Sven 2011 年 11 月 2 日

1 投票

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
Walter Roberson 2011 年 11 月 2 日
Those would be horizontal lines, not vertical lines...
Sven
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
Walter Roberson 2011 年 11 月 2 日
Confusing...
Sven
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
James 2014 年 3 月 28 日

1 投票

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
Wayne King 2011 年 11 月 2 日

0 投票

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
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 で Geographic Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by