MATLAB: Plotting time/date on surf or mesh plot

date time surfaceplot

I'm trying to plot time&date on a surface but can't get it to look right. It's a time period between 1920-2019 where measurements are scattered in between those years, som years more measurements than other years. Dates where in Julian days to start with and have been converted to datetime. Which I have tried to plot it in, but couldnt get to work.
After that, I tried with just years. This looked fine on the labels, but with all the data from that year in spot didnt look very representative. I also tried converting YYMMDD format and it didn't look very good either.
Any advice on how i could do this? It would be greatly appreciated.

Best Answer

surf can handle datetime and duration data. Let's create some sample datetime data and use that to compute a duration.
[d1, d2] = meshgrid(datetime('now')+days(0:10));
z = d1-d2;
Plot it as a surface.
h = surf(d1, d2, z);
I would prefer the Z axis labels to be in units of days rather than hours, minutes, and seconds. Let's change the format on those labels. First we need to get the axes that contains the surface plot.
ax = ancestor(h, 'axes');
Now set the format of the Z axis labels to 'd' to show them in units of days.
Technically I didn't need to get the axes handle ax, but this ensures that if there are multiple axes open (potentially in multiple figures) I'm changing the format for the correct axes.
ztickformat(ax, 'd')
Related Question