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

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

 Respuesta aceptada

Star Strider
Star Strider el 29 de Oct. de 2014
Editada: MathWorks Support Team el 17 de Mzo. de 2021
That’s easy enough with the quiver function:
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.

8 comentarios

Amit K
Amit K el 29 de Oct. de 2014
Movida: Dyuman Joshi el 21 de Dic. de 2023
Thank you Star Strider.
It did work but how can I make the arrowhead bigger? In addition the head is not facing outward so it head to tell if the head is there.
AK
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 times larger.
Jérôme
Jérôme el 22 de Nov. de 2022
Movida: Dyuman Joshi el 21 de Dic. de 2023
@Star Strider, out of curiosity, how did you know the property MaxHeadSize?
Because it is not mentionned in the page https://www.mathworks.com/help/matlab/ref/quiver.html.
Steven Lord
Steven Lord el 22 de Nov. de 2022
Movida: Dyuman Joshi el 21 de Dic. de 2023
It is not mentioned directly on the quiver function documentation page, but that page does state in the Description section:
"quiver(___,Name,Value) specifies quiver properties using one or more name-value pair arguments. For a list of properties, see Quiver Properties. Specify name-value pair arguments after all other input arguments. Name-value pair arguments apply to all of the arrows in the quiver plot."
MaxHeadSize is one of the properties in the Arrows section on the Quiver Properties page. This is a common pattern for graphics objects with many properties: rather than having an exhaustive list on the documentation page for the function that creates the graphics object put those properties on a separate page. See the Graphics Object Properties category page for a list of those property pages.
Jérôme
Jérôme el 22 de Nov. de 2022
Movida: Dyuman Joshi el 21 de Dic. de 2023
@Steven Lord Indeed, I missed it. Thank you.
Star Strider
Star Strider el 22 de Nov. de 2022
Movida: Dyuman Joshi el 21 de Dic. de 2023
@Steven Lord — Thank you! (I was off doing other things for a few minutes.)
Thank you!
Another option —
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)
scatter([p1(1) p2(1)], [p1(2) p2(2)], 125, 'r', 's', 'filled')
grid
axis([0 10 0 10])
text(p1(1),p1(2), sprintf(' (%.0f,%.0f)',p1), 'Horiz','left')
text(p2(1),p2(2), sprintf(' (%.0f,%.0f)',p2), 'Horiz','left')
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim;
yl = ylim;
pos = gca().Position;
annotation('arrow', xapf([p1(1) p2(1)],pos,xl), yapf([p1(2) p2(2)],pos,xl))
The ‘xapf’ and ‘yapf’ anonymous functions make annotation objects easier to work with. (I devised them a few months ago.)
Their entire code requires all these assignments:
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim;
yl = ylim;
pos = gca().Position;
They need all those variables. This example works as documentation for their use.
.

Iniciar sesión para comentar.

Más respuestas (4)

Sajeer Modavan
Sajeer Modavan el 14 de Mzo. de 2019
Editada: Sajeer Modavan el 14 de Mzo. de 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 comentario

Could you give an describtion of how the arrow coordinates are set exactly? x and y need to be between 0 and 1 within annotation, so if I want the arrow to go from e.g. (x1,y1) = (3,0.5) and (x2,y2) = (6,0.5) how is x and y adjusted for exact location?

Iniciar sesión para comentar.

The following code (Matlab R2019a) draws an arrow in the plot coordinates from point P1 to point P2 using standard Matlab code. Zoom in/out shifts position of the arrow.
P1=[10, -1]; % from point
P2=[70, 2];
figure;hold on
plot(P1(1), P1(2), 'o')
plot(P2(1), P2(2), 'o')
%xlim([5 80])
%ylim([-5 5]) % must be set before the conversion below
ax=gca;
Pos=ax.Position;
Xlim=xlim;
Ylim=ylim;
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));
annotation('arrow', X_conv, Y_conv)

2 comentarios

How did you get Pos = [0.10 0.55 0.85 0.4];
The code was updated, thanks.

Iniciar sesión para comentar.

Amit K
Amit K el 30 de Oct. de 2014
Editada: Amit K el 30 de Oct. de 2014
Thank you guys. It worked but not good enough for what I'm doing with the plots. Is it possible to draw an arc instead of a line between the two point going from the starting point to the end point. The two points are not equidistant from the centre. Appreciate for your help.
AK

Categorías

Más información sobre Vector Fields en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 28 de Oct. de 2014

Comentada:

el 22 de Oct. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by