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 views (last 30 days)
Show older comments
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')
Accepted Answer
Walter Roberson
on 13 Sep 2022
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 Comments
dpb
on 15 Sep 2022
...
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.
More Answers (0)
See Also
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!