952 views (last 30 days)
Show older comments
FC93 on 7 Oct 2016
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot
Commented: JORGE ORDOÑEZ CARRASCO on 23 Nov 2021
Accepted Answer: Marc Jakobi
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.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Marc Jakobi on 7 Oct 2016
Edited: MathWorks Support Team on 28 Nov 2018
Open in MATLAB Online
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 Comments Show 4 older commentsHide 4 older comments
Show 4 older commentsHide 4 older comments
FC93 on 7 Oct 2016
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_396748
Exactly, thank you. Now I included all the horizontal lines I needed.
Stephen on 23 Jan 2019
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_663528
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 on 22 Jul 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_945396
Thx a lot!
Sabrina Vasiliadis on 11 Feb 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_1322272
Can you add characteristics to this line somehow? I want it to be a black solid line.
Steven Lord on 11 Feb 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_1322412
Open in MATLAB Online
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).
JORGE ORDOÑEZ CARRASCO on 23 Nov 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_1848624
Open in MATLAB Online
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')
Sign in to comment.
More Answers (2)
Massimo Zanetti on 7 Oct 2016
Open in MATLAB Online
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 Comments Show NoneHide None
Show NoneHide None
FC93 on 7 Oct 2016
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_396749
Thank you for your help.
Rolando Murilli on 16 Aug 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_975066
Thanks!!
Sign in to comment.
KSSV on 7 Oct 2016
Open in MATLAB Online
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])
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
FC93 on 7 Oct 2016
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/306204-add-various-horizontal-lines-to-a-plot#comment_396750
Thank you for your help.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and Annotation
Find more on Formatting and Annotation in Help Center and File Exchange
Tags
- horizontal line
- plot
- avoid overfitting
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office