MATLAB Answers

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

617 views (last 30 days)
Clemens
Clemens on 5 Nov 2016
Commented: Brent F on 15 Jun 2021 at 23:53
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 Comment
Clemens
Clemens on 5 Nov 2016
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. 

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 5 Nov 2016
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 Comments
Star Strider
Star Strider on 20 Sep 2017
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!

Sign in to comment.

More Answers (1)

Jorg Woehl
Jorg Woehl on 1 Mar 2021
Edited: Jorg Woehl on 1 Mar 2021
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.
  4 Comments
Brent F
Brent F on 15 Jun 2021 at 23:53
Excellent answer - much better than doing the conversion ourselves each time.
Preferably, we'd just specify the arrowhead x,y + label text.
function [] = LabelArrow (fig, x, y, labelStr)
a = annotation('textarrow');
a.Parent = fig.CurrentAxes;
a.X = [x x]; a.X = .90*a.X; a.X(1) = .8*a.X(1);
a.Y = [y y];
a.String = labelStr;
a.LineWidth = 1;
end
But the method of chosing the starting point and direction the arrow points above is a hack and doesn't always look right.
Is there any tool/plugin that handles figuring out where the text should go in order to avoid overwriting the graph and drawing the arrow from there?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!