Converting plot coordinates to normalized coordinates

49 ビュー (過去 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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by