I have datetime scale on x-axis and numeric values in Y-axis. And I want plot arrows like this

댓글 수: 2

Adam Danz
Adam Danz 2021년 4월 6일
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'?
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일

0 개 추천

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

This is my actual plot and I want to make xlines at three periods in the time series at certain dates and in between plots I want to mark the arrows. So can you please tell me how can I use your code to this plot.
Adam Danz
Adam Danz 2021년 4월 7일
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 2021년 4월 7일
I've updated my answer with a second demo using datetime values.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

태그

질문:

2021년 4월 6일

편집:

2021년 4월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by