Add various horizontal lines to a plot

How can I add various horizontal lines to a plot?
I have a plot and now I want to add several horizontal lines. I would like to draw a horizontal line between x=-6 to x=-2 and another horizontal line between ×=3 and x=10.
Could someone show me a way to do it? Thank you.

 채택된 답변

Marc Jakobi
Marc Jakobi 2016년 10월 7일
편집: MathWorks Support Team 2018년 11월 28일

25 개 추천

If you want the line to have specific end points, you can use the line function. For example, this code draws a horizontal line at y = 5 between the points x = -6 and x = -2.
y = 5;
line([-6,-2],[y,y])
Starting in R2018b, you can use the xline and yline functions to draw vertical and horizontal lines, respectively. For example, this code draws a horizontal line at y = 5. The horizontal line extends in both the positive and negative directions with no end points.
yline(5)
For more information on the yline function, see: https://www.mathworks.com/help/matlab/ref/yline.html

댓글 수: 6

FC93
FC93 2016년 10월 7일
Exactly, thank you. Now I included all the horizontal lines I needed.
Stephen
Stephen 2019년 1월 23일
This helped me, too.
Is is also possible to draw a horizontal line of specified length BELOW a figure? I have a heatmap that plots certain data versus time on the x axis. I'd like to add a calibration bar just below the x axis.
Steeven Lopez
Steeven Lopez 2020년 7월 22일
Thx a lot!
Can you add characteristics to this line somehow? I want it to be a black solid line.
plot(1:10)
h = yline(5, 'r--', 'LineWidth', 4);
You can change various properties of the line using its handle, or you can set those properties when the line is constructed like I did with the linespec ('r--') and the property names (LineWidth).
Hello i have a question about this topic what if a need severel ylines, spaced every 100 unitos and my axis goes from 0 to 2500, as you can see bellow, i have to add every 100 units a line and name it, is there a short cut cause i need to do this at least 23 times. Thank you
hold on
yline(244.229,'--','Lat 37')
hold on
yline(356.5,'--','Lat 38')
hold on
yline(468.7,'--','Lat 39')
hold on
yline(580.916,'--','Lat 40')
hold on
yline(693.145,'--','Lat 41')
hold on
yline(805.373,'--','Lat 42')
hold on
yline(917.602,'--','Lat 43')

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Massimo Zanetti
Massimo Zanetti 2016년 10월 7일

23 개 추천

Horizontal line at what y coordinate? Fix y and then plot the line, for example if y=5:
x=1:12;
y=5;
plot(x,y*ones(size(x)))

댓글 수: 2

FC93
FC93 2016년 10월 7일
Thank you for your help.
Thanks!!

댓글을 달려면 로그인하십시오.

KSSV
KSSV 2016년 10월 7일

1 개 추천

x=linspace(-6,-2,M) ;
%%y range
N = 50 ;
y = linspace(-5,5,N) ; % you have to select y range
for i = 1:N
xi = x ;
yi = y(i)*ones(size(xi)) ;
plot(xi,yi,'r')
hold on
end
x=linspace(3,10,M) ;
%%y range
N = 50 ;
y = linspace(-5,5,N) ;
for i = 1:N
xi = x ;
yi = y(i)*ones(size(xi)) ;
plot(xi,yi,'r')
hold on
end
xlim([-10 40])

카테고리

도움말 센터File Exchange에서 Labels and Styling에 대해 자세히 알아보기

질문:

2016년 10월 7일

댓글:

2021년 11월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by