How do you make a line with an arrow halfway between 2 points?

조회 수: 4 (최근 30일)
Kulan Sinclair
Kulan Sinclair 2018년 7월 10일
편집: jonas 2018년 7월 10일
i'm looking for something like. [p1] ------>-----[p2]

답변 (1개)

jonas
jonas 2018년 7월 10일
편집: jonas 2018년 7월 10일
Let's say you want an arrow from between [X1 X2],[Y1 Y2]. We use annotation but specify the arrow coordinates by the axes coordinates (instead of the default figure coordinates)
x=0:.05:2*pi;
y=sin(x);
h=plot(x,y)
X=[0 4]; %[X1 X2]
Y=[0.5 -0.5]; %[Y1 Y2]
Pos=get(gca,'position');
xlimits=get(gca,'xlim');
xmin=xlimits(1);
xmax=xlimits(2);
AxesRangeX=diff(xlimits)
FigPosX=Pos(1)+(X-xmin)*(Pos(3)/AxesRangeX)
ylimits=get(gca,'ylim');
ymin=ylimits(1);
ymax=ylimits(2);
AxesRangeY=diff(ylimits)
FigPosY=Pos(2)+(Y-ymin)*(Pos(4)/AxesRangeY)
dX=diff(FigPosX);
dY=diff(FigPosY);
annotation('arrow',FigPosX,FigPosY,'headstyle','none')
annotation('arrow',FigPosX-[0 dX./2],FigPosY-[0 dY./2])
  댓글 수: 2
Kulan Sinclair
Kulan Sinclair 2018년 7월 10일
the problem with annotate is it ignores the bounds of the figure and doesn't operate with the same units as the rest of the figure(or plots contained within it) it's easy enough to scale a number so it is a ratio between 0 and 1 but wen 0 is some indeterminate distance outside the plot and 1 is also outside the plot. scaling it to fit within the bounds of a plot when i don't know how large the plot is going to be relative to the size of the figure is difficult. i'm thinking quiver is going to be my best option but i was hoping to a different linestyle for a normal plot.
jonas
jonas 2018년 7월 10일
편집: jonas 2018년 7월 10일
I've updated the code. You can now plot the arrow by specifying values in reference to the axes.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by