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
채택된 답변
추가 답변 (4개)
Sajeer Modavan
2019년 3월 14일
편집: Sajeer Modavan
2019년 3월 14일
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
Marius Hammer
2019년 4월 26일
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?
Image Analyst
2014년 10월 29일
4 개 추천
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.
Vitaly Fedoseev
2021년 5월 26일
편집: Vitaly Fedoseev
2024년 10월 22일
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
Brian LaRocca
2023년 12월 21일
How did you get Pos = [0.10 0.55 0.85 0.4];
Vitaly Fedoseev
2024년 10월 22일
The code was updated, thanks.
댓글 수: 1
Image Analyst
2014년 10월 30일
Attach your screenshot.
카테고리
도움말 센터 및 File Exchange에서 Vector Fields에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

