How can I plot arrows on date time plot ?
11 views (last 30 days)
Show older comments
Jeevan Kumar Bodaballa
on 6 Apr 2021
I have datetime scale on x-axis and numeric values in Y-axis. And I want plot arrows like this
2 Comments
Adam Danz
on 6 Apr 2021
This doesn't look like a datetime plot.
Are the x values actual datetime or duration values or are you proving numeric values that you're calling 'datetime'?
Accepted Answer
Adam Danz
on 6 Apr 2021
Edited: Adam Danz
on 7 Apr 2021
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 Comments
Adam Danz
on 7 Apr 2021
Why didn't you provide this as the example in your question? It would have same much time to provide an accurate example.
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!