Shade standard deviation for time series

Hi!
I'm trying to shade the std (daily_std) for mean sst (daily_average).
I've seen similar questions, but those codes don't work for me.
My x-axis is one year of date times. My y axis is the mean of sst.
I've done the following code:
figure
curve1=daily_average(:,1)+daily_std(:,1); %only column 1 because every column is one different location
curve2=daily_average(:,1)-daily_std(:,1);
plot(date,curve1,'r')
hold on
plot(date,curve2,'r')
hold on
date2 = [date, fliplr(date)];
inBetween = [curve1, fliplr(curve2)];
fill(date2, inBetween, 'g');
hold on;
plot(date, daily_average(:,1));
However, I obtain the following result:
Any idea?
Thank you very much

 Accepted Answer

I cannot run your code, so I cannot test this.
However, fill may not be appropriate here. See if using patch instead does shat you want:
patch(date2, inBetween, 'g');
Your curtrent syntax will work for patch, so no other changes are necessary.

5 Comments

But my vector of dates is in date format, so I receive the following error:
"Non-numeric data is not supported in 'patch'"
However, I've tried datenum(date2) but I receive the same message
N=1:366;
date=datetime(2020,1,N);
date=transpose(date);
daily_average=table2array(daily_average);
daily_std=table2array(daily_std);
figure
curve1=daily_average(:,1)+daily_std(:,1); %above curve
curve2=daily_average(:,1)-daily_std(:,1); %below curve
plot(date,curve1,'r')
hold on
plot(date,curve2,'r')
hold on
date2 = [date, fliplr(date)];
inBetween = [curve1, fliplr(curve2)];
patch(date2, inBetween, 'g');
% fill(date2, inBetween, 'g');
hold on;
plot(date, daily_average(:,1));
The only option appears to be to convert the datetime array to date numbers for the plot, then use patch.
Example —
dt = datetime('now')+calmonths(1:10);
dn = datenum(dt);
upper = rand(1,10)+2;
lower = rand(1,10);
figure
patch([dn fliplr(dn)], [upper fliplr(lower)], 'g')
datetick('x', 'mmm', 'keepticks','keeplimits')
EDIT — (10 Jul 2020 at 17:39)
This works, and produces the correct result with datetime vectors with my code:
figure
fill([dt, fliplr(dt)], [upper, fliplr(lower)].', 'g')
grid
so with respect to your code, try this:
fill(date2, inBetween.', 'g');
I rarely use fill, preferring patch, however it seems that leaving the first argument (independent variable vector) as a row vector and transposing the second argument (dependent variable vector) to a column vector will do what you want with fill. This is not in the documentation (at least I cannot find any reference to it), and simply required a bit of experimentation for me to discover it.
It produces the same result as the patch call with a datenum vector, without the inconvenience of having to convert the datetime vector to datenum.
.
Thank you very much!!!! It worked!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!