How to precisely place textarrow?

2 views (last 30 days)
winkmal on 7 Sep 2022
Answered: dpb on 7 Sep 2022
Inside a plot, I want to place several textarrows to mark specific events on the timeline. However, their placement is not very precise. Consider this MWE:
clc; clearvars
startEndDtStr = ["01.08.2022 06:00:00", "11.08.2022 06:00:00"];
startEndDateTime = datetime(startEndDtStr);
daysTotal = days(diff(startEndDateTime));
dtVector = startEndDateTime(1):hours(1):startEndDateTime(2);
%% Create plot
fh1 = figure('Position', [180 112 1150 650]);
plot(dtVector, sinpi(days(dtVector - startEndDateTime(1)))+1, 'LineWidth',1.5);
hold on; grid on
xArrow1 = days(datetime(2022,8,6,6,0,0) - startEndDateTime(1))/daysTotal;
annotation('textarrow', [xArrow1+.03 xArrow1], [1.7 1.0]./2, 'String',"Start Event", 'FontSize',14);
set(gca, 'FontSize',16);
% exportgraphics(fh1,'textArrowPlacement.png', 'Resolution',300)
Which produces this plot:
As you can see, the tip of the arrow does not end at [0.5, 0.5] in relative coordinates, or [August 06/2022/06:00, 1] in absolute coordinates. I suppose it has something to do with the plot area.
What would be the code to get it there?

Accepted Answer

dpb on 7 Sep 2022
This has always been a pain in the royal patootie...the annotaton objects are in figure coordinates while the data inside the plot is in axes data units.
One has to retrieve the axes position inside the figure and scale to the relative position in the figure. That TMW doesn't even supply a builtin routine for this is still a huge hole.
There are at least the following FEX submissions in replacement <Generalized-and-vectorized-2-d-arrows-plot> and <arrow>

More Answers (0)




Community Treasure Hunt

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

Start Hunting!