253 views (last 30 days)
Show older comments
Emmanuel on 29 May 2014
Commented: Peter Hansen on 17 Aug 2021
Accepted Answer: David Sanchez
I am given two points (x1,y1) and (x2,y2). How can I plot a line that will pass through these two points and extend till the x and y axis?
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
David Sanchez on 29 May 2014
Open in MATLAB Online
If you want a line connecting A and B:
A = [2 3];
B = [4 5];
plot(A,B,'*')
axis([0 10 0 10])
hold on
line(A,B)
hold off
If you want a line through A and B that extend to the plt limits:
xlim = get(gca,'XLim');
m = (B(2)-B(1))/(A(2)-A(1));
n = B(2)*m - A(2);
y1 = m*xlim(1) + n;
y2 = m*xlim(2) + n;
hold on
line([xlim(1) xlim(2)],[y1 y2])
hold off
5 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
Emmanuel on 29 May 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_216600
Hey!! Thanks a lot! Now my lines are intersecting when I changed the xlim from [-10 10] from your code. But the form a triangle in the negative axis
Tunhe Zhou on 19 Apr 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_446847
n = B(2)*m - A(2); This seems to be n = B(2) - A(2)*m;
Goku on 29 Aug 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_480372
Thank you. But How can this be done in 3D ?
nobody on 11 Sep 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_1005073
above meaning connecting the two points P1=[A(1),B(1)] and P2=[A(2),B(2)]
If you want to connect the two points C=[2,3] and D=[4,5] you do
m = (D(2)-C(2)) / (D(1)-C(1)); %slope
n = C(2) - C(1)*m %vertical shift from [0,0]
y1 = m*xlim(1) + n;
y2 = m*xlim(2) + n;
don't you?
Ronaldo Lim on 27 Nov 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_1164153
excuse me, why do you use "vertival shift from [0,0]" . What's that ?
Sign in to comment.
More Answers (1)
Mahesh on 29 May 2014
A = (x1,y1); B = (x2,y2);
plot(A,B)
4 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Mahesh on 29 May 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_216591
Example: Want to draw the line b/w the points (5,10) and (15,30).
A = (5,15); B = (10,30); plot(A,B)
Emmanuel on 29 May 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_216597
monospaced Thanks!!
Adam Danz on 4 Jun 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_1563740
Edited: Adam Danz on 4 Jun 2021
Open in MATLAB Online
No, this is incorrect. With the syntax plot(A,B), A contains x-values and B contains y-values. Instead, Mahesh defined A and B as (x,y) coordinates.
A simple test will show that this is incorrect,
A = [1,2];
B = [ -3,4];
plot(A,B)
hold on
plot(A(1),A(2), 'r*')
plot(B(1),B(2), 'k*')
legend('line','A','B','Location','BestOutside')
axis padded
To connect coordinates A-B you must use,
plot([A(1),B(1)], [A(2),B(2)])
Peter Hansen on 17 Aug 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/131603-how-can-we-plot-a-line-passing-through-two-points#comment_1692942
Open in MATLAB Online
First of all, you need to understand that mathlab is made for matrixes and arrays, to easy big data calculation.
So all data is ussaly needed in matrix or arrays.
Therfor when ploting points, you dont plot single points like A and B but and array of x-cordinates of A and B and a array of y-cordinates of A and B.
maybe this will claryfy it for you :)
figure(2); clf(2); hold on; axis([0 10 0 10]); axis padded
% If Point A is in x=1 and y=2
% If Point B is in x=-3 and y=4
% If Point C is in x=-6 and y=-4
x_array = [1 -3 -6];
y_array = [2 4 -4];
plot(x_array(1),y_array(1),'*') % A
plot(x_array(2),y_array(2),'*') % B
plot(x_array(3),y_array(3),'*') % C
% OR to plot all points at once uncoment below line insted, and remve ",'B','C'" from legend
% plot(x_array,y_array,'*') % A, B and C
line(x_array,y_array) % line
legend('A','B','C','line','Location','BestOutside') hold off
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABData Import and AnalysisVisual Exploration
Find more on Visual Exploration in Help Center and File Exchange
Tags
- lines
- 2d plots
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)
Europe
- 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