필터 지우기
필터 지우기

Axis position changes when i use rectangle

조회 수: 7 (최근 30일)
Jesse Desterro
Jesse Desterro 2021년 3월 20일
댓글: Jesse Desterro 2021년 3월 22일
Hi there,
I'm trying to draw a rectangle and insert some annotations, but it seems that drwaing a rectangle change the axis position and when i try to annotate i can't draw arrows in position I want.
% drawing a rectangle:
fig = figure;
set(fig, 'WindowState','maximized','Color',[1 1 1])
rectangle('Position', [0 0 700 60], 'LineWidth',2, 'FaceColor',[0.7 .7 .7])
axis equal
set(gca,'DataAspectRatio',[1 0.5 1]);
set(gcf, 'WindowState','maximized','Color',[1 1 1])
get(gca, 'position')
returns: [0.1300 0.1100 0.7750 0.8150], but it seems nor right for me.
% EXAMPLE: Inserting annotation in limits of given gca position:
nxi = 0.13; nxf = 0.7750; nyi = 0.1100; nyf = 0.8150;
annotation('doublearrow',[nxi 1],[.5 .5])
I wanted the arrows to be in this case exactly in 0 and 700 of my axis, in this case. i want to draw annotations inside my figure and it's drawing in the wrong positions.

채택된 답변

dpb
dpb 2021년 3월 21일
편집: dpb 2021년 3월 21일
I've NEVER understood (and never will understand) the annotation implementation only in absolute units of reference relative to the figure coordinates and no option for datacentric placement relative to the axes. It's mind-bogglingly obtuse and mostly of little use as implemented. And, to compound the problem, absolutely none of the examples show how to get from one to the other to do something as simple as what you're looking for...they all have "magic numbers" buried in them that were picked to work for the sake of pretty illustrations, but no guidance on how the values were arrived at.
The problem is you've got to scale the coordinates of the axes from their data values on the axes to the corresponding position in the figure coordinates in order to be able to put the annotation where you want it in a coordinate system in which values are known...
Here's an example to get what you're looking for...it can be packaged a little more elegantly, but let's you see what's needed this way--
hAx=axes; % create axes, save handle
xlim([0 700]),ylim([-120 175]) % set limits wanted
hR=rectangle('Position',[0,0,700,60]); % draw the rectangle, data coordinates
pos=hAx.Position; % retrieve the axes position in figure
yAnn=interp1(ylim,[pos(2) pos(2)+pos(4)],[20]); % find the y==20 position in figure coordinates
hA=annotation('doublearrow',[pos(1) pos(1)+pos(3)],[yAnn yAnn]); % draw annotation there
Remember the position 4-vector is [left bottom width height] so x positions are [left left+width]
The above produces:
Again, why in the world one can't set the 'Units' property to 'data' and reference the axes for the container totally incomprehensible to me.
ADDENDUM:
To draw at a given x position other than the axes limits, you'd interpolate on the x position values just as did above for yAnn, of course. These are ideal candidates to be written as anonymous functions to generalize.
ADDENDUM SECOND:
"I've NEVER understood ... the annotation implementation only in absolute units of reference relative to the figure coordinates ..."
Well, the above is not quite true, I understand the "why" -- because the annotation object is a child of the figure and cannot be a child of an axes. Hence, the units are those of the parent.
More accurately, what I do not understand is if there is some reason why TMW cannot also make provisions to have the axes accept an annotation as a child, why on earth they didn't provide the high-level pieces needed to be able to use what they did provide in a user-friendly manner.
Sure, it's doable, but why should the user have to do it -- MATLAB should live up to the rapid-development environment that handles stuff like this you for you so you don't have to.
ADDENDUM THIRD:
And, it's clear I'm not alone...some 15 others had already viewed the Q? with not even a comment before I came along.
  댓글 수: 3
dpb
dpb 2021년 3월 21일
편집: dpb 2021년 3월 21일
What's wrong with using my above code with rectangle? It uses the same four values you're passing and will draw the annotation where you want it vertically if you also pass that y value (I chose 20 for illustration), or it can be the middle arbitrarily as you had done above if that is the way you're sure you'll always want it. Or, it could be an optional parameter and be the middle if not passed, that location if given.
Jesse Desterro
Jesse Desterro 2021년 3월 22일
There is no problem using your code. Your code works perfectly. I created the function before you answer

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

추가 답변 (0개)

카테고리

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