Converting plot coordinates to normalized coordinates

49 visualizzazioni (ultimi 30 giorni)
Glenn
Glenn il 16 Dic 2014
Modificato: KSSV il 4 Dic 2023
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!

Risposte (1)

Glenn
Glenn il 16 Dic 2014
Modificato: Glenn il 16 Dic 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);

Community Treasure Hunt

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

Start Hunting!

Translated by