How do I regulate datetime x-axis when using subplots?

So I'm formatting plots by year into a subplot but I keep on having about 1/4 of the subplot's x-axis be completely wrong. There's only four ticks instead of the twelve I need. The four ticks are also in the completely wrong place, April is in October for example.
Here's the part of my code I'm concerned with:
subplot(4,2,j)
pcolor(datetime(timedata,'ConvertFrom','datenum'),data1,data2)
xstart = datetime(iyr(j),01,1);
xend = datetime(iyr(j),12,31,23,59,59);
xlim([xstart,xend])
xtickformat("MM")
xticklabels(["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"])
In this case, xlim seems to be the problem but I need it to regulate other subplots.
Any ideas on why this is happening and how to fix it? Thank you!

2 件のコメント

Cris LaPierre
Cris LaPierre 2022 年 7 月 21 日
Could you share an image of when things are incorrect?
Diego
Diego 2022 年 7 月 21 日
編集済み: Diego 2022 年 7 月 21 日
In the picture, you can slide right and it shows the incorrect format I'm getting.

サインインしてコメントする。

 採用された回答

Cris LaPierre
Cris LaPierre 2022 年 7 月 21 日

0 投票

Thank you.
Does your timedata correspond to your ticklabels? Why not use xtickformat('MMM') instead of manually specifying the xtick labels?
timedata = datetime(2021,1:12,1);
pcolor(timedata,rand(10,1),rand(10,12))
xstart = datetime(2021,01,1);
xend = datetime(2021,12,31,23,59,59);
xlim([xstart,xend])
xtickformat("MMM")

3 件のコメント

Diego
Diego 2022 年 7 月 21 日
Oops I was unaware of this tiny specification, thank you!
Although it's aligned correctly now, is there a reason it still only shows 4 ticks for some of the plots?
Steven Lord
Steven Lord 2022 年 7 月 21 日
What does xticks return for the axes that only has four tick labels shown?
plot(1:10);
xticks(1:3:10)
xticklabels(string(1:10))
whereAreTheTicksLocated = xticks
whereAreTheTicksLocated = 1×4
1 4 7 10
That's correct based on my previous call to xticks.
Cris LaPierre
Cris LaPierre 2022 年 7 月 21 日
The number of ticks used are automatically chosen. You can override this by specifying the xtick locations manually using xticks.
xticks(xstart:calmonths(1):xend)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2022 年 7 月 21 日

コメント済み:

2022 年 7 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by