How to precisely place textarrow?

조회 수: 11 (최근 30일)
winkmal
winkmal 2022년 9월 7일
답변: dpb 2022년 9월 7일
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);
xlim(startEndDateTime);
xticks(startEndDateTime(1):days(2):startEndDateTime(2));
set(gca, 'FontSize',16);
% exportgraphics(fh1,'textArrowPlacement.png', 'Resolution',300)
Which produces this plot:
textArrowPlacement
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?

채택된 답변

dpb
dpb 2022년 9월 7일
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>

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by