In addition, any suggestion on how to extent the interval between the ticks? t shows intervals of 15min. It would be nice if it would only show the day and with a time interval of 6 hours...
Imagesc with datetime on x axis
24 次查看(过去 30 天)
显示 更早的评论
I'd like to use imagesc to plot a matrix with distance on y axis, z in colorbar, and datetime on x axis. Y and Z are in the matrix (12 x 500) and datetime is in seperate vector t (12 x 1).
I have the following code but the datetime that shows up on the x axis is January 1, January 2, ... (from 1 - 12).
If I add t, the imagesc doesn't show up.
How can I plot the 12 x 500 matrix and plot t as x axis?
figure
imagesc(D', 'Interpolation', 'bilinear');
set(gca, 'YDir', 'normal');
colorbar
set(gca, 'XTick', t);
datetick('x', 'mmmm dd HH:MM', 'keepticks')
采纳的回答
Walter Roberson
2022-9-13
Unfortunately, it is not supported to use datatime() objects for imagesc axes.
datetick() is not for datetime() objects: it is for the older serial date numbers, which is numerically days and fractions of a day since a particular starting point.
If your t is datetime() objects then use
tnum = datenum(t);
imagesc(D', 'Interpolation', 'bilinear', 'XData', tnum);
set(gca, 'YDir', 'normal')
colorbar
xticks(tnum);
datetick('x', 'mmmm dd HH:MM', 'keepticks')
8 个评论
dpb
2022-9-15
...
N = height(tnum);
for i = 1:N
if any(isnan(tnum(:,i)))
D(i,:)=NaN;
end
end
...
in MATLAB is simply written as
D(any(isnan(tnum(:,i))),:)=NaN;
using vectorized logical addressing.
But, you don't need to do that, anyways, if either x or y variable is not finite the HG2 routines won't show anything for those locations so simply the bad x value will be sufficient.
However, once you created the timetable, the time variable is a datetime and so it will be NAT and you'll need isnat instead of isnan
BUT, there's still the problem that visually this will just create bands on the axes in those locations; it won't collapse the axis; it will still be continuous over the full time span.
It takes one of the ways of adjusting the axes or the times themselves to create the effect desired. I've not tried the FEX submission linked to, in the past the one I tried had some warts; don't recall if the same or not; even if was those may have been fixed by now so give it a shot as the simplest route forward until, at least, it's shown not to be adequate.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geographic Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!