필터 지우기
필터 지우기

Specify annotation position with respect to x- and y-axes values?

조회 수: 251 (최근 30일)
Clemens
Clemens 2016년 11월 5일
댓글: Swaroop 2022년 11월 8일
Dear all,
Is there a way the define the starting and end point of an annotation object such as an arrow in terms of the data being plotted and not as either "normalized" coordinates (x- and y-positions between 0 and 1) or a physical length measurement such as cm?
E.g. I am plotting a time from 0 to 180 s on the x-axis and a concentration between 0 and 0.1 concentration units on the y-axis. Now I would like to draw an arrow starting at 90 s, 0.04 concentration units and going to 120 s, 0.09 concentration units.
I have already considered to simply "normalize" those positions myself (e.g. 90 s/180 s = 0.5 --> x-position in normalized coordinates). This does, however, not work, since 0 and 1 refer to points in the whole figure (the window in which the plot appears) and not to the area of the plot itself.
Thanks for any input! Clemens
  댓글 수: 1
Clemens
Clemens 2016년 11월 5일
Here's a quick illustration of what I mean. I simply drew the arrow by hand. If I get Matlab to show me the code for it, it gives
annotation(figure1,'arrow',[0.354235423542354 0.224422442244224],...
[0.526863777089783 0.390092879256966]);
but the end point's x-coordinate (0.2244...) does not correspond to the 20 s (20 s / 180 s = 0.1111...) where 180 s is the "length" of my x-axis in the units that I want to plot. 

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

채택된 답변

Star Strider
Star Strider 2016년 11월 5일
You need to access the 'Position' property of the figure object you’re using. It gives the left lower corner coordinates and the width and height values. With a few lines of code, you can normalise your arrow object positions with respect to them. See the documentation on Axes Properties for details.
Then, experiment! This will keep you intensely occupied for a few minutes!
  댓글 수: 4
DrBones
DrBones 2017년 9월 20일
편집: DrBones 2018년 2월 27일
Edit: I am terribly sorry, both for writing things I could not make sense of a couple months later and for not replying sooner Star Strider. Apparently, I did not get notified.
This comment is replaced with a way too long new comment which hopefully makes up for it since it also scales to negative x and hopefully isn't totally out of place here. My apologies in advance if it is.
% define domain and function
x = linspace(-10, 10, 100);
sigma = 3;
gaussian = exp(-x.^2/(2*sigma^2));
% plot the function and some labels
figure
plot(x, gaussian)
hold on
xlabel('x')
ylabel('y')
HM = 0.5 % Half of maximum
HWHM = 3.53 % Half width at half maximum
pos = get(gca, 'Position');
% Positins for the end of the Arrow in data units.
xPosition = 2;
yPosition = 0.3;
% Create a textarrow annotation at the coordinates in data units
% the textarrow coordinates are given [end_x, head_x], [end_y, head_y]
ta1 = annotation('textarrow',...
[(xPosition + abs(min(xlim)))/diff(xlim) * pos(3) + pos(1),...
(HWHM + abs(min(xlim)))/diff(xlim) * pos(3) + pos(1) ],...
[(yPosition - min(ylim))/diff(ylim) * pos(4) + pos(2),...
(HM - min(ylim))/diff(ylim) * pos(4) + pos(2)]);
% The data units are rescaled to normalized units for x via
% x_norm = ( x_data + abs(min(xlim)))/diff(xlim) * pos(3) + pos(1)
% and for y via ( does NOT work for negative values (see x for this)
% y_norm = (y_data - min(ylim))/diff(ylim) * pos(4) + pos(2)
% Set some text
ta1.String = ['HWHM: ', num2str(HWHM, 3), ];
% Also for negative number now :). "line" accepts the same style
% coordinates
Start = [-8, 0.5]; % x, y
End = [4, 0.1]; % x , y
ta2 = annotation('line',...
[(End(1) + abs(min(xlim)))/diff(xlim) * pos(3) + pos(1),...
(Start(1) + abs(min(xlim)))/diff(xlim) * pos(3) + pos(1) ],...
[(End(2) - min(ylim))/diff(ylim) * pos(4) + pos(2),...
(Start(2) - min(ylim))/diff(ylim) * pos(4) + pos(2)]);
This results in
Thanks again for all the help!
Also unrelated
My orthopedic knowledge is, apart from my namesake, unfortunately rather lacking. I am more involved in the imaging side :)
Star Strider
Star Strider 2017년 9월 20일
More information would be helpful, as would more illustrative example code, since we don’t know what you’re doing, and providing a context is always appropriate. I assume ‘x_data’ is your independent variable vector. Also, is this robust to ‘x_data’ having negative values?
Unrelated
‘DrBones’ — Ortho? B/C IM here!

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

추가 답변 (2개)

Jorg Woehl
Jorg Woehl 2021년 3월 1일
편집: Jorg Woehl 2021년 3월 1일
This is an older post, but the following is perhaps useful to others:
If horizontal arrows are ok, data units can be used by associating the annotation with the current axes:
hf = figure;
x = 0:180; f = @(x) 0.09*exp(-x/18);
plot(x, f(x));
ha = annotation('arrow');
ha.Parent = hf.CurrentAxes; % associate annotation with current axes
% now you can use data units
ha.X = [50 20];
ha.Y = [f(20) f(20)];
This also works for other annotation types and even if they don't have a horizontal orientation. (Arrows and doublearrows are an exception because the arrowheads will not align properly.)
Otherwise, there is also the excellent coord2norm function on File Exchange that converts data units to normalized units.
  댓글 수: 9
Trevor
Trevor 2022년 8월 10일
Excellent addition, thanks so much!
Swaroop
Swaroop 2022년 11월 8일
Thank you very much!

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


DGM
DGM 2021년 8월 15일
As a spin off @Jorg Woehl's suggestion, I'll offer my own.
I think it's worth pointing out that these solutions tend to be version-dependent -- at least dependent on whether we're post-R2014b or not. Obviously, we can't manipulate properties of annotation objects using dot notation in legacy versions, but even using get/set to move the annotation tends to result in it being drawn in unexpected ways.
But precalculating the position and specifying it when creating the annotation works fine from R2009b to R2019b:
clf
x = 0:180; f = @(x) 0.09*exp(-x/18);
plot(x, f(x));
[xx yy] = datc2figc([50 20],[f(20) f(20)]);
annotation('arrow',xx,yy);
I used the attached file for coordinate conversion, but I doubt it's much different than coord2norm().
  댓글 수: 3
Marc Compere
Marc Compere 2021년 8월 15일
it's bizarre that data coordinates are not built into Matlab directly. Its the single most straightforward use for an arrow annotation.
DGM
DGM 2021년 8월 15일
datc2figc() was really just an ad-hoc thing I threw together in frustration. I'm sure there's better stuff out there.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by