![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288508/image.png)
Properly put date labels with OuterPosition and datetick
4 ビュー (過去 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
![](https://i.imgur.com/A6ZGI32.png)
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
![](https://i.imgur.com/1J4MJX0.png)
How to properly set the x labels?
This is my matlab version ![](https://i.imgur.com/pSHxAtj.png)
![](https://i.imgur.com/pSHxAtj.png)
0 件のコメント
採用された回答
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')))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288508/image.png)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Axis Labels についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!