Plot series with strips in the background

8 views (last 30 days)
Linden
Linden on 18 May 2014
Commented: Linden on 19 May 2014
Hi I want plot a time series (see below) with shade strips in the background. For example, if the data belongs to 1998, add a light grey strip behind the line. If it belongs to 1999, add a darker grey stripe behind the line and so on. I would also like to have a text box on each strip indicating the period of time. It seems to be a very challenging task for me. can someone help me?
thanks very much.
'6/01/1998' [ 5.41]
'9/01/1998' [5.0933]
'12/01/1998' [ 4.39]
'3/01/1999' [4.6633]
'6/01/1999' [ 4.88]
'9/01/1999' [ 5.16]
'12/01/1999' [5.6067]
'3/01/2000' [6.1867]
'6/01/2000' [6.2167]
'9/01/2000' [ 6.13]
'12/01/2000' [ 5.9]
'3/01/2001' [4.5967]
'6/01/2001' [ 3.78]

Answers (2)

dpb
dpb on 18 May 2014
Edited: dpb on 18 May 2014
OK, I did take a few minutes -- I'm certainly not adroit w/ patches so this is crude at best--but it gets the ideas across for starters, I think.
dn=datenum(dat(:,1),'m/dd/yyyy'); % create datenums for your time vector
plot(dn,[dat{:,2}].') % and plot the data
xtk=datenum(1998,[1:6:13*4-1].',1); % tickmarks for quarters for yeears
xlim([xtk(1) xtk(end)]) % set limits to match
set(gca,'xtick',xtk) % and the ticks
datetick('x',17,'keeplimits','keepticks') % date format axis
xdata=[[xtk(1);xtk(3);xtk(3);xtk(1)] [xtk(3);xtk(5);xtk(5);xtk(3)]];
ydata=repmat([4.5 4.5 5.5 5.5].',1,2);
cdata=[0.8 0.8 0.8]; % a gray color, fairly dark
for i=1:size(xdata,2) % each year group defined
p(i)=patch(xdata(:,i),ydata(:,i),cdata); % draw each year patch
set(p,'facealpha',0.3+0.1*i); % alpha variably lighter to darker
end
The loop is where undoubtedly a better implementation could be made--I wasn't expert-enough in a short time to be able to fixup the alpha globally so took the easy way out of making multiple objects. That surely isn't necessary w/ a little more finesse...
doc patches % for all the skinny--happy reading! :)
ADDENDUM:
Oops, forgot about the text labels for the years...
xyr=datenum([1998:1999]',6,1); % get the 1/1 year locations
text(xyr,5.5,datestr(xyr,'yyyy'), ...
'horizontalalign','center','verticalalign','bottom')

Star Strider
Star Strider on 18 May 2014
It took a while to get the patch working correctly, thus the delay.
This code:
C = {'6/01/1998' [ 5.41]
'9/01/1998' [5.0933]
'12/01/1998' [ 4.39]
'3/01/1999' [4.6633]
'6/01/1999' [ 4.88]
'9/01/1999' [ 5.16]
'12/01/1999' [5.6067]
'3/01/2000' [6.1867]
'6/01/2000' [6.2167]
'9/01/2000' [ 6.13]
'12/01/2000' [ 5.9]
'3/01/2001' [4.5967]
'6/01/2001' [ 3.78]};
Cd = datenum(C(:,1), 'mm/dd/yyyy');
Cn = cell2mat(C(:,2));
Cv = datevec(Cd);
[Cyu, ia, ic] = unique(Cv(:,1));
ia = [ia; size(Cd,1)];
figure(1)
plot(Cd, Cn, '-g')
datetick('x', 'mm/dd/yyyy')
cm = colormap(gray(size(ia,1)+1))*0.20+0.40
ysv = ylim;
hold on
for k1 = 1:size(ia,1)-1
X = [Cd(ia(k1)) Cd(ia(k1)) Cd(ia(k1+1)) Cd(ia(k1+1)) Cd(ia(k1))]'
Y = [ysv fliplr(ysv) ysv(1)]'
patch(X, Y, cm(k1,:))
end
plot(Cd, Cn, '-g', 'LineWidth',2)
hold off
produced this plot:
You may want to experiment with the colormap line to get the result you want. I plotted the line in green in order to make it easily visible against the background.

Community Treasure Hunt

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

Start Hunting!