Converting plot coordinates to normalized coordinates

79 views (last 30 days)
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!

Answers (1)

Glenn
Glenn on 16 Dec 2014
Edited: Glenn on 16 Dec 2014
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);

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!