plot annotation without normalizing

조회 수: 19 (최근 30일)
Prince Chowdhury
Prince Chowdhury 2018년 4월 1일
편집: Prince Chowdhury 2018년 4월 2일
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

답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 1일
https://www.mathworks.com/matlabcentral/fileexchange/63760-annotate
  댓글 수: 1
Prince Chowdhury
Prince Chowdhury 2018년 4월 2일
편집: Prince Chowdhury 2018년 4월 2일
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!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by