Wednesday, April 11, 2012

plot extending several years

In the following example I have two years worth of data denoted by data_2007 and data_2008 which have a corresponding array of dates:


clear all
 DateTime_2007 = datestr(datenum('2007-01-01 00:00','yyyy-mm-dd HH:MM'):1/24:...
     datenum('2007-12-31 23:57','yyyy-mm-dd HH:MM'),...
     'yyyy-mm-dd HH:MM');
 DateTime_2007 = cellstr(DateTime_2007);
 
 DateTime_2008 = datestr(datenum('2008-01-01 00:00','yyyy-mm-dd HH:MM'):1/24:...
     datenum('2008-12-31 23:57','yyyy-mm-dd HH:MM'),...
     'yyyy-mm-dd HH:MM');
 DateTime_2008 = cellstr(DateTime_2008);
 
 data_2007 =  1 + (20-1).*rand(8760,1);
 data_2008 = 1 + (20-1).*rand(8784,1);
 


I would like to plot the data on one graph, showing how the data has varied over the 2 years, so basically a plot extending over 2 years. How can this be achieved, considering that I need the dates to be shown along the xaxis, and possible only the month name (mmm) to be given (not yyyy-mm-dd HH:MM).


Answer:


Have you tried the datetick command to label the axes?
plot(datenum(DateTime_2007),data_2007)
hold on
plot(datenum(DateTime_2008),data_2008,'g')
datetick('x','mmm')
It might be easier if you didn't convert your dates to date strings, and then have to convert them back for plotting.

No comments:

Post a Comment