How can I plot arrows on date time plot ?

13 vues (au cours des 30 derniers jours)
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa le 6 Avr 2021
Modifié(e) : Adam Danz le 7 Avr 2021
I have datetime scale on x-axis and numeric values in Y-axis. And I want plot arrows like this
  2 commentaires
Adam Danz
Adam Danz le 6 Avr 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'?
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa le 6 Avr 2021
That plot was a sample and I have data which is datetime on x-axis.

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 6 Avr 2021
Modifié(e) : Adam Danz le 7 Avr 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 commentaires
Adam Danz
Adam Danz le 7 Avr 2021
Why didn't you provide this as the example in your question? It would have same much time to provide an accurate example.
Adam Danz
Adam Danz le 7 Avr 2021
I've updated my answer with a second demo using datetime values.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by