Matlab Graphic Abstract

I'm just wondering how to add annotation to the 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 this? And any better idea for plot annotation?

Thank you and welcome!


EDIT:

I just saw from a reference document:

the 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 units .

In my code, I would like the arrow to point to the point (x, y), which is drawn by the graph (), but the annotation interprets the values โ€‹โ€‹of x and y, as in normalized units of the figure. Therefore, I think this is causing the problem. How to specify the correct coordinates for annotation?

+4
source share
3 answers

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

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

axPos [xMin,yMin,xExtent,yExtent]

Then you get the limits, i.e. minimum and maximum axes.

 xMinMax = xlim; yMinMax = ylim; 

Finally, you can calculate the annotation x and y from the graph of 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 the x and y coordinates for your annotation.

+7
source

Another way to get the normalized coordinates of the figures is to use the data view to convert the unit of measure units (ds2nfu) to FileExchange.

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

I had some problems with understanding the normalized coordinates, until I realized that the coordinates (0,0) and (1,1) correspond to the lower left corner and upper right corner of the COMPLETE plot window, and not just the plot, Below is a snippet and A screenshot can help others who wondered where 0 starts and 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)

+1
source

Source: https://habr.com/ru/post/1306412/


All Articles