How can I plot arrows on date time plot ?

17 ビュー (過去 30 日間)
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa 2021 年 4 月 6 日
編集済み: Adam Danz 2021 年 4 月 7 日
I have datetime scale on x-axis and numeric values in Y-axis. And I want plot arrows like this
  2 件のコメント
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa 2021 年 4 月 6 日
That plot was a sample and I have data which is datetime on x-axis.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2021 年 4 月 6 日
編集済み: Adam Danz 2021 年 4 月 7 日
You can use annotation objects whose positions are defined in normalized figure space. That requires you to convert the end points from data units to normalized figure units which is done in the demos below. Critically, pay attention to the inline comments because once the conversion is done, you cannot change the position of the axes relative to the figure and you cannot change the axis limits. The offset variable determines the vertical space between the upper axis and the arrows. The arrowEnds variable defines the endpoints of the arrows.
Demo with duration values
fig = figure();
ax = axes('Units','Normalize'); % important: normalized units!
time = seconds(0:100:5000);
data = rand(size(time));
plot(time,data);
grid(ax, 'on')
% Define start/stop of horizontal arrows in data units.
arrowEnds = [1000, 2000; 2000, 4000]; % nx2 array of n [start, end] values.
% IMPORTANT
% set axis limits and add any legends and colorbars before proceeding!
% The axis should not change position relative to the figure after here.
xlim(ax, [min(time), max(time)])
ylim(ax, ylim(ax))
% Convert arrowEnds from data to normalized fig units
axPos = ax.Position;
xl = seconds(ax.XLim); % Convert to numeric
yl = ax.YLim;
arrowEndsAxisNorm = arrowEnds./range(xl) + xl(1);
arrowEndFigNorm = arrowEndsAxisNorm.*axPos(3) + axPos(1);
offset = 0.025; % vertical offset from top of axes (normalized units)
yFigNorm = (sum(axPos([2,4])) + offset);
% Add arrow annotations
ah = gobjects(1,size(arrowEndFigNorm,1));
for i = 1:size(arrowEndFigNorm,1)
ah(i) = annotation(fig, 'doublearrow', arrowEndFigNorm(i,:), [yFigNorm,yFigNorm]);
end
Demo with datetime values
fig = figure();
ax = axes('Units','Normalize'); % important: normalized units!
time = datetime(1999,01,01) + days(1:10:500);
data = rand(size(time));
plot(time,data);
grid(ax, 'on')
% Define start/stop of horizontal arrows in data units.
% nx2 array of n [start, end] values in datetime.
arrowEnds = [datetime(1999,04,01), datetime(1999,10,01);
datetime(1999,10,01), datetime(2000,04,01)];
% IMPORTANT
% set axis limits and add any legends and colorbars before proceeding!
% The axis should not change position relative to the figure after here.
xlim(ax, xlim(ax));
ylim(ax, ylim(ax))
% Convert arrowEnds from data to normalized fig units
axPos = ax.Position;
xl = ax.XLim;
yl = ax.YLim;
arrowEndsAxisNorm = (arrowEnds-xl(1))/range(xl);
arrowEndFigNorm = arrowEndsAxisNorm.*axPos(3) + axPos(1);
offset = 0.025; % vertical offset from top of axes (normalized units)
yFigNorm = (sum(axPos([2,4])) + offset);
% Add arrow annotations
ah = gobjects(1,size(arrowEndFigNorm,1));
for i = 1:size(arrowEndFigNorm,1)
ah(i) = annotation(fig, 'doublearrow', arrowEndFigNorm(i,:), [yFigNorm,yFigNorm]);
end
  3 件のコメント
Adam Danz
Adam Danz 2021 年 4 月 7 日
I've updated my answer with a second demo using datetime values.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by