annotation in matlab plot

18,608

Solution 1

First, you need to find the position of the axes in normalized figure units. Fortunately, they're set to 'normalized' by default.

axPos = get(gca,'Position'); %# gca gets the handle to the current axes

axPos is [xMin,yMin,xExtent,yExtent]

Then, you get the limits, i.e. min and max of the axes.

xMinMax = xlim;
yMinMax = ylim;

Finally, you can calculate the annotation x and y from the plot x and y.

xAnnotation = axPos(1) + ((xPlot - xMinMax(1))/(xMinMax(2)-xMinMax(1))) * axPos(3);
yAnnotation = axPos(2) + ((yPlot - yMinMax(1))/(yMinMax(2)-yMinMax(1))) * axPos(4);

Use xAnnotation and yAnnotation as x and y coordinates for your annotation.

Solution 2

Another way to get normalized figure coordinates is to use Data space to figure units conversion (ds2nfu) submission on FileExchange.

[xa ya] = ds2nfu(x,y);

Solution 3

I had some trouble understanding the normalized coordinates, until I realized that the coordinates (0,0) and (1,1) are respectively the lower left corner and upper right corner of the COMPLETE plot window, not just of the plot. The below snippet and the screenshot might help others who have been wondering where the 0 starts and the 1 ends.

x = -1:0.1:1;
y = x.^2;
plot (x,y)
xlabel('time [s]')
ylabel('amplitude')
title('My nice plot')
legend('y(t)')
grid on
annotation('arrow', [0 1], [0 1])

Plot with arrow coordinates (0,0) and (1,1)

Share:
18,608
Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on June 04, 2022

Comments

  • Tim
    Tim almost 2 years

    I just wonder how to add annotation in matlab plot? Here is my code:

    plot(x,y);  
    annotation('textarrow',[x, x+0.05],[y,y+0.05],'String','my point','FontSize',14);
    

    But the arrow points to the wrong place. How can I fix it? And any better idea for annotating a plot?

    Thanks and regards!


    EDIT:

    I just saw from the help document:

    annotation('line',x,y) creates a line annotation object that extends from the point defined by x(1),y(1) to the point defined by x(2),y(2), specified in normalized figure units.

    In my code, I would like the arrow pointing to the point (x,y) that is drawn by plot(), but annotation interprets the values of x and y as in normalized figure units. So I think that is what causes the problem. How can I specify the correct coordinates to annotation?