Properly put date labels with OuterPosition and datetick

1 次查看(过去 30 天)
I'm trying to change labels on x axis to be from 24/2/2020 to today (actually, at least 1 day after today, otherwise the curve will touch the y axis on the right). I have to use subplot since I have other plots, and I have to use OuterPosition (or similar?) since the figure contains also annotations.
The following code
subplot(3,2,1)
past = datenum('02-24-2020');
present = datenum(datetime('today')+1); % +1 so that curve won't touch y axis on right
plot(linspace(past,present,65), 1:65)
set(gca, 'OuterPosition',[.1 .1 .4 .3], 'XLim',[past present])
datetick('x','dd/mm','keepticks')
produces this
which is not correct, since it cuts data on both on left and on right. The result doesn't change by removing the subplot command.
Even if I remove the OuterPosition option from set, the result doesn't change.
If i remove both subplot and OuterPosition, the result improves a bit, but it is still wrong
How to properly set the x labels?
This is my matlab version

采纳的回答

Mehmed Saad
Mehmed Saad 2020-4-29
编辑:Mehmed Saad 2020-4-29
You can define xtick and xticklabel by yourself
subplot(3,2,1)
past = datenum('02-24-2020');
present = datenum(datetime('today')+1); % +1 so that curve won't touch y axis on right
plot(linspace(past,present,65), 1:65)
Suppose i want 5 ticks between past and present
tick_pos = linspace(past,present,5);
Set xtick property equal to tick_pos. and for xticklabels convert tick_pos to datestr and then to cell.
set(gca, 'OuterPosition',[.1 .1 .4 .3],'XLim',[past present],...
'XTick',tick_pos,'XTickLabel',cellstr(datestr(tick_pos,'dd/mm')))
  1 个评论
giannit
giannit 2020-4-29
编辑:giannit 2020-4-29
Thank you very much! The curve still touches the y axis though, to correct it we have to use 'today' in the linspace for the plot, and keep 'today+1' elsewhere, right?
Moreover, is it possible to place 'today' as last label, while keeping the curve far from the y axis, i.e. while keeping XLim(2) = 'today+1' ?
EDIT: got it!
subplot(3,2,1)
past = datenum('02-24-2020');
pres0 = datenum(datetime('today'));
pres1 = datenum(datetime('today')+1);
plot(linspace(past,pres0,65), 1:65)
tick_pos = linspace(past,pres0,5);
set(gca, 'OuterPosition',[.1 .1 .4 .3],'XLim',[past pres1],...
'XTick',tick_pos,'XTickLabel',cellstr(datestr(tick_pos,'dd/mm')))

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by