plotting data from two years with day of year replacing julian date

2 次查看(过去 30 天)
I have the following code for producing a plot of data that extends two years in terms of day of year:
time = datenum('2008-04-17 02:00'):datenum('2009-11-24 12:27');
dateV = datevec(time);
for i = 1:length(time);
DOY(i) = time(i) - datenum(dateV(i,1),0,0);
end
data = rand(length(time),1);
plot(time,data);
set(gca,'XTick',floor(time(1:50:end))','XTickLabel',floor(DOY(1:50:end)))
Could someone suggest a method for ensuring the ticks on the xaxis are for day numbers that are multiples of 10 i.e. 110, 160 etc.

采纳的回答

Star Strider
Star Strider 2012-9-14
编辑:Star Strider 2012-9-15
Does this do what you want (placed after your for looop)?
data = rand(length(time),1) + sin(2*pi*DOY'/365)/5;
StartDOY = 200;
InitDOYIdx = find(DOY >= StartDOY, 1, 'first')
PlotIdx = [InitDOYIdx:length(DOY)]';
plot(time(PlotIdx),data(PlotIdx));
xtiklocs = floor(time(InitDOYIdx:50:end))';
xtiklbls = 10*floor(DOY(InitDOYIdx:50:end)/10)';
set(gca,'XTick',xtiklocs,'XTickLabel',xtiklbls)
Produces:
Dates xtiklocs xtiklbls
2008 07 18 733607 200
2008 09 06 733657 250
2008 10 26 733707 300
2008 12 15 733757 350
2009 02 03 733807 30
2009 03 25 733857 80
2009 05 14 733907 130
2009 07 03 733957 180
2009 08 22 734007 230
2009 10 11 734057 280
I changed data a bit to provide a trend as a check to be sure different start times plotted appropriately. (I set it arbitrarily to start at the beginning of 2008.)
I broke out ‘xtiklocs’ and ‘xtiklbls’ as separate variables so I could keep track of them, and coded ‘xtiklbls’ to produce only multiples of 10, as you requested.
I made ‘InitDOYIdx’ the index of the first occurrence of the 200th day because you asked for that as an illustration. You can of course define it with ‘StartDOY’ to be any ‘DOY’ index that works with your data.

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-9-14
close
time = datenum('2008-04-17 02:00'):datenum('2009-11-24 12:27');
dateV = datevec(time);
for i = 1:length(time);
DOY(i) = time(i) - datenum(dateV(i,1),0,0);
end
data = rand(length(time),1);
n=length(time)
plot(1:n,data);
set(gca,'XTick',10:50:n)
  5 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2012-9-14
when you say 200, compared to what, what is your mark? how can we know that is 200, then we can fix our code
Richard
Richard 2012-9-15
So, the original time series is for julian date, I want to show the xaxis as day of year so I calculate 'DOY'. I then plot the data against julian date ('time') and then convert the xaxis to DOY. The day number therefore depends on the original time vector (of julian dates) which could start on any day of the year (50,200,250 and so on). Basically instead of the code you have provided above I was looking for a solution that moved the xticks to the location where the first DOY was a multiple of 10 and then did the same for the remaining ticks. Hope this makes sense.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by