Can "datetime" be responsible for the misalignment of data in a plot ?
2 次查看(过去 30 天)
显示 更早的评论
I have two types of time vectors, t1 and t2, both indicating a time range of 44 days:
% vector t1
t1 = linspace(0,44,1057);
>> t1
t1 =
1056×1 single column vector
0
0.04166667
0.08333334
0.125
0.1666667
0.2083333
0.25
0.2916667
0.3333333
...
43.83331
43.87498
43.91665
43.95831
% vector t2
a = {'17-Jun-2017'; '30-Jul-2017'};
b = cellstr(datetime(a{1}) : days(1) : datetime(a{end}))
k = 1;
for i = 1:length(b)
for j = 0:23
t2{k} = strjoin([b(i),sprintf('%d:00:00',j)]);
k = k + 1;
end
end
>> t2'
ans =
1056×1 cell array
{'17-Jun-2017 0:00:00' }
{'17-Jun-2017 1:00:00' }
{'17-Jun-2017 2:00:00' }
{'17-Jun-2017 3:00:00' }
{'17-Jun-2017 4:00:00' }
{'17-Jun-2017 5:00:00' }
...
{'30-Jul-2017 19:00:00'}
{'30-Jul-2017 20:00:00'}
{'30-Jul-2017 21:00:00'}
{'30-Jul-2017 22:00:00'}
{'30-Jul-2017 23:00:00'}
When I plot the same data, but with different types of time arrays as x-axis
% I plot both types of times:
subplot(2,1,1);
plot(datetime(t2), Prob);
subplot(2,1,2);
plot(t1, Prob);
I can see that my data in the first subplot and in the second subplot, where I used the two different types of time vectors, are not aligned on the x-axis, i.e. the time-axis (from the red lines that I have added with powerpoint we can see that the same peaks do not correspond in the time-axis).... Can "datetime" be responsible for the misalignment of data ?
0 个评论
采纳的回答
Star Strider
2022-5-18
The first cluster of data (at about 2.5 or about 18 Jun) appears to be perfectly aligned in both plots. The datetime plot goes to 05 Aug, while the ‘t1’ plot stops at 45.
See if setting:
xlim([min(t1) max(t1)])
and
xlim([min(t2) max(t2)])
in their respective plots corrects the misalignment.
.
更多回答(1 个)
Cris LaPierre
2022-5-18
I think the allignment difference is due to what MATLAB is setting your axis limits to when the x values are datetimes (Jun 17 - Aug 5, or 50 days) vs when they are doubles (0-45).
Set your axes limits to be the same number of days in both plots, and I'd expect the data to align.
另请参阅
类别
在 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!