# How can I draw a line with arrow head between 2 data points in a plot

2,561 views (last 30 days)

Show older comments

Hi,

If there are 2 points in XY plane [x1, y1] and [x2, y2] then how can I draw a line with an arrowhead starting from [x1, y1] and ending at [x2, y2]. Basically, I want the plot to look like this:

[x1, y1]--------->[x2, y2]

Preferably, I like the arrow-line to bend (arc) towards the second point.

AK

##### 0 Comments

### Accepted Answer

Star Strider
on 29 Oct 2014

Edited: MathWorks Support Team
on 17 Mar 2021

p1 = [2 3]; % First Point

p2 = [9 8]; % Second Point

dp = p2-p1; % Difference

figure

quiver(p1(1),p1(2),dp(1),dp(2),0)

grid

axis([0 10 0 10])

text(p1(1),p1(2), sprintf('(%.0f,%.0f)',p1))

text(p2(1),p2(2), sprintf('(%.0f,%.0f)',p2))

The final zero in the quiver call turns off the automatic scaling.

The arc would be a bit more challenging, depending on what you intend by arc. I would just use the quiver result.

##### 0 Comments

### More Answers (5)

Sajeer Modavan
on 14 Mar 2019

Edited: Sajeer Modavan
on 14 Mar 2019

t = 1:0.1:10; wo = 1;

x = 2*exp(t*wo).*sin(t*wo);

plot(t,x),hold on

plot([6 6],[-1e5 1e5],'--k','Linewidth',1.4)

plot([8.7 8.7],[-1e5 1e5],'--k','Linewidth',1.4)

ylim([-0.3e5 1e4])

% One arrow from left to right with text on left side

x = [0.74 0.79]; % adjust length and location of arrow

y = [0.3 0.3]; % adjust hieght and width of arrow

annotation('textarrow',x,y,'String',' Growth ','FontSize',13,'Linewidth',2)

% Arrow with two head at both end and text between

y = [0.4 0.4];

Xadj = 1.35; % adjust location of left arrow starting point (the sum of this with 'x' should not be negative)

annotation('textarrow',x,y,'String',' Growth ','FontSize',13,'Linewidth',2)

annotation('textarrow',-x+Xadj,y,'String','','FontSize',14,'Linewidth',2)

% One arrow from left to right with text over it

x = [0.56 0.79]; % adjust length and location of arrow

y = [0.5 0.5];

annotation('textarrow',x,y,'FontSize',13,'Linewidth',2)

annotation('textbox',[.6 .3 .7 .27],'EdgeColor','none','String','Growth','FontSize',13,'Linewidth',2)

##### 1 Comment

Marius Hammer
on 26 Apr 2019

Image Analyst
on 29 Oct 2014

The "Official solution" from the folks at the Mathworks: http://www.mathworks.com/matlabcentral/answers/92988-how-do-i-plot-a-line-that-has-an-arrow-on-the-end-of-it-in-2-d-and-3-d

I use arrow3 from the File Exchange.

##### 0 Comments

Amit K
on 29 Oct 2014

Edited: Amit K
on 29 Oct 2014

##### 1 Comment

Star Strider
on 29 Oct 2014

My pleasure!

I don’t know what you mean by ‘the head is not facing outward’. It’s the normal behaviour of the quiver plot. If you want to increase the size of the arrowhead, specify 'MaxHeadSize' as larger than the default value of 0.2. For example:

quiver(p1(1),p1(2),dp(1),dp(2),0, 'MaxHeadSize',0.5)

makes it 2½ times larger.

Vitaly Fedoseev
on 26 May 2021

Edited: Vitaly Fedoseev
on 26 May 2021

The following code (Matlab R2019a) draws an arrow in the plot coordinates from point P1 to point P2. Zoom in/out shifts position of the arrow:

P1=[10,-1]; %from point

P2=[70,2]; % to point

figure;

Xlim=[-1 110];

Ylim=[-2 3];

Pos = [0.10 0.55 0.85 0.4];

subplot('Position', Pos)

hold on

X_conv(1)=Pos(1)+(Pos(3))/(Xlim(2)-Xlim(1))*(P1(1)-Xlim(1));

X_conv(2)=Pos(1)+(Pos(3))/(Xlim(2)-Xlim(1))*(P2(1)-Xlim(1));

Y_conv(1)=Pos(2)+(Pos(4))/(Ylim(2)-Ylim(1))*(P1(2)-Ylim(1));

Y_conv(2)=Pos(2)+(Pos(4))/(Ylim(2)-Ylim(1))*(P2(2)-Ylim(1));

x=0:0.1:100;plot(x, sin(x));plot([-100 1000], P2(2)*[1 1]); plot(P2(1)*[1 1], [-100 100]);

plot(x, sin(x));plot([-100 1000], P1(2)*[1 1]); plot(P1(1)*[1 1], [-100 100])

xlim(Xlim)

ylim(Ylim)

annotation('arrow', X_conv, Y_conv)

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!