# plotting datenums to compare data across years

9 views (last 30 days)
Ryan on 14 Sep 2016
Commented: Brendan Hamm on 15 Sep 2016
So while I have plenty of experience plotting things in Matlab, I'm not very familiar with plotting non-numeric stuff like datetime class variables. I like that there are some built-in things to make plots look nice for datetime variables (it will choose what ticks to display nicely, even with zooming, etc.), but it doesn't seem to work to plot multiple arrays of datetime/data pairs on a single plot. In fact, the line() notation doesn't even work with datetime variables.
A little background, I have a whole bunch of numeric data that corresponds to an average or a total of some quantity over the course of a day (such as temperature, humidity, etc.), with corresponding dates going back for years (although not necessarily covering the same date ranges from year to year). I would like to compare daily numbers over the course of the years, but can't seem to find a good way to use the datetime variables such that you get the same nice time scaling with zoom, etc., but on the x-axis show only the label for the month/day (so you can show multiple years on the plot) rather than month/day/year. I hope this makes sense.
The following code is a snippet of what I might start out trying to do:
date = datetime(2015,01,01):1:datetime(2016,09,13); % Generate some date range
y = 20*rand(size(date)); % Generate some random data
ind2016 = find(date.Year >= 2016,1,'first'); % Get the index of the year change
hl1 = plot(date(1:ind2016-1),y(1:ind2016-1)); hold on; % Plot year 1
hl2 = plot(date(ind2016:end),2*y(ind2016:end)); % Plot year 2 (the 2x is just so there is a visible difference between year 1 and 2)
Obviously, if I plot this, I just end up with a plot that joins year 1 and 2 end to end.
Instead, I would like to alter the datetime variables to only include month/day of some arbitrary year so that I can just overlay the two. Something like this:
date(1:ind2016-1).Year = 2000;
date(ind2016:end).Year = 2000;
figure,
hl1 = plot(date(1:ind2016-1),y(1:ind2016-1)); hold on; % Plot year 1
hl2 = plot(date(ind2016:end),2*y(ind2016:end)); % Plot year 2 (the 2x is just so there is a visible difference between year 1 and 2)
dateFormat = 'mmm dd'; % mm
datetick('x',dateFormat)
But then the nice zooming / rescaling of the dates doesn't work anymore. And this way feels a little hacky.
I'm thinking there is a much better way I am simply overlooking that will allow me to overlay data from different years without resorting to the datetick function which breaks the nice automatic datetime formatting that you get with the plot command alone. Thanks in advance to anybody that takes a look at this!

Brendan Hamm on 14 Sep 2016
You are mixing functionality for datenums ( datetick ) with datetimes so this is why you get the re-scaling you mention.
% Extract portions for ease of reading
y1 = y(1:ind2016-1);
d1 = date(1:ind2016-1);
y2 = 2*y(ind2016:end);
d2 = date(ind2016:end);
% Change the Year (Atlernative way using datetime arithmetic)
yearsDiff = year(d2) - year(d1);
d2 = d2 - calyears(yearsDiff);
% Use the DatetimeTickFormat option to change the display
% Note: The symbols are a little different then with datetick as they now conform to ISO standards
plot(d1,y1,d2,y2,'DatetimeTickFormat','MMM dd')
Peter Perkins on 15 Sep 2016
Following up on Brendan's suggestion, there's not currently a way to make a datetime plot that "overlays" multiple years. What he's done is to shift everything to the same year, and then only show the month and day. This works, mostly, except for leap days. The calyears subtraction will account for that in a sensible way, but you'll end up with either a duplicate day (if you shift a leap year to a non-leap year) or a gap (if the other way around). You'll likely want to patch that up somehow.
Hope this helps.
Brendan Hamm on 15 Sep 2016
Nice suggestion on the leap year. I can honestly say I didn't consider that here.

### Categories

Find more on Axis Labels 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!