plot annotation without normalizing

50 views (last 30 days)
Prince Chowdhury
Prince Chowdhury on 1 Apr 2018
Edited: Prince Chowdhury on 2 Apr 2018
Hello,
I am trying to position my annotation arrows onto a 2D XY plot but it seems that annotation x and y points can only be in normalized units. I would like to place my annotation arrows using the X and Y values that I used for plotting the data. Can someone please help me to convert the data values to normalized values? I have gone through a whole bunch of posts in Matlab and stack overflow but nothing is working. The below code should have worked but it didn't.
"
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."
So, from this code I understand that "xPlot" and "yPlot" are supposed to represent the databvalues from your plot that you want to convert to normalized unit. However, when I replace the "xPlot" and "yPlot" with specific data value from my plot such as 100 and 200 respectively then my yAnnotation gives a value of 18 and xAnnotation gives a value of 5.3 which is not within the range of 0 to 1!!!!
Can someone please please help me!! Many thanks

Answers (1)

Walter Roberson
Walter Roberson on 1 Apr 2018
https://www.mathworks.com/matlabcentral/fileexchange/63760-annotate
  1 Comment
Prince Chowdhury
Prince Chowdhury on 2 Apr 2018
Edited: Prince Chowdhury on 2 Apr 2018
Hi, I am not fully understanding this function. I am just interested in converting the data values to normalized values. Is this part of the code from "Annotate.dsxy2figxy(varargin)" sufficient to convert the values to normalized values?
"% Transform from data space coordinates to normalized figure coordinates
hFig = ancestor(hAx,'figure');
if exist('x','var') % Transform and return a pair of points figx = (x - axlim(1)) / axwidth * axpos(3) + axpos(1); figy = (y - axlim(3)) / axheight * axpos(4) + axpos(2); for i = 1:numel(figx) figpos(i,:) = hgconvertunits(hFig, [figx(i),figy(i),0,0], 'pixels', 'normalized', hContainer); %#ok<AGROW> end varargout{1} = figpos(:,1)'; varargout{2} = figpos(:,2)'; else % Transform and return a position rectangle figpos(1) = (pos(1) - axlim(1)) / axwidth * axpos(3) + axpos(1); figpos(2) = (pos(2) - axlim(3)) / axheight * axpos(4) + axpos(2); figpos(3) = pos(3) * axpos(3) / axwidth; figpos(4) = pos(4) * axpos(4) / axheight; varargout{1} = hgconvertunits(hFig, figpos, 'pixels', 'normalized', hContainer); end"
Thanks!!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!