Converting plot coordinates to normalized coordinates

조회 수: 48 (최근 30일)
Glenn
Glenn 2014년 12월 16일
편집: KSSV 2023년 12월 4일
I'm trying to add arrows pointing to data points (start and end of arrow found using "ginput(2)") and add a text label at the end of the arrow. Because the ginput points are in respect to the axes of the plot and the normalized points (used with the "annotation" command) are in respect to the whole figure, I can't figure out how to convert from plot coordinates to normalized so that I can use the annotation function. Is there a property for the plot portion of the figure so that I can find out where the axes limits lie in the figure?
[x,y]=ginput(2); %start and end of arrow
Xrange=max(get(AX(1),'Xlim'))-min(get(AX(1),'Xlim'));
Yrange=max(get(AX(1),'Ylim'))-min(get(AX(1),'Ylim'));
X=(x-min(get(AX(1),'Xlim')))/Xrange +min(get(AX(1),'Xlim'))/Xrange;
Y=(y-min(get(AX(1),'Ylim')))/Yrange +min(get(AX(1),'Ylim'))/Yrange;
annotation('textarrow', X, Y,'String' , LegendText.Fig1{i},'Fontsize',12);
Thanks!

답변 (1개)

Glenn
Glenn 2014년 12월 16일
편집: Glenn 2014년 12월 16일
More generic version of code:
[x,y]=ginput(2); %start and endof arrow
AX=axis(gca); %can use this to get all the current axes
Xrange=AX(2)-AX(1);
Yrange=AX(4)-AX(3);
X=(x-AX(1))/Xrange +AX(1)/Xrange;
Y=(y-AX(3))/Yrange +AX(3)/Yrange;
annotation('textarrow', X, Y,'String' , 'LegendText','Fontsize',12);

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by