MATLAB Answers


Shade area between confidence intervals (ecdf)

Asked by Panos
on 24 Mar 2013

I am using the ecdf command to plot empirical cdfs with their associated confidence intervals. I want to fill in/shade the confidence interval area.

This is the code i have used for the confidence intervals:

data1 = xlsread('File1.xlsx');
[F,X,Flo1,Fup1] = ecdf(data1);
hold on;
hold off

and this is the code using shadedplot:

ha=shadedplot(X,Flo1',Fup1', [0.2 0.7 0.7], 'g');

I could not get it to work however. It still gives me the confidence intervals but they are not shaded in. Any help will be greatly appreciated (I'm a beginner in Matlab!)



No products are associated with this question.

1 Answer

Answer by the cyclist
on 24 Mar 2013
Edited by the cyclist
on 24 Mar 2013
 Accepted answer

Do you know if the shadedplot() command you are using is this one from the File Exchange:

It is a little difficult to help you debug this, because we don't have your data (and we don't have your function unless we download it).

I can suggest a few things, though. It looks like shadedplot() is supposed to plot the lines as well as the shaded area. But you are also plotting the lines with separate plot commands. Do you get the lines from shadedplot if you do not do the other plot commands?

You could try to see if you can post a small sample of your data that still exhibits the problem. Then, we can at least to replicate the problem (if people are willing to download the FEX code). This simplification may actually also help you see what is causing the problem.

Another thing I would suggest is using debug mode to step into the code and see if you can tell why the shading isn't working. There are detailed instructions on how to do that here: (This is something that is good for any beginner to learn!)


on 25 Mar 2013

thank you very much for your quick response.

yes, the shadedplot command i am using is the one in your link.

I tried the shadedplot command with and without the other plot commands, and it gives me the same result. My data is just a column vector, i post here below a sample of it with the code i used, with and without the other plot commands:

clear;clc;close all
x=[276,222,346.153,623.0769, 143.5294, 340,179,145,184.6154,256,380.7692,...
%without other plot commands
[F,X,Flo,Fup] = ecdf(x);
shadedplot(X,Flo',Fup',[.5 .5 .5],'r');
hold off;
%with other plot commands
plot(X,Flo,'r-'); hold on;
plot(X,Fup,'r-'); hold on;
shadedplot(X,Flo',Fup',[.5 .5 .5],'r');
hold off;

thank you for the link on debug mode

the cyclist
on 25 Mar 2013

I found the problem. Your Flo and Fup variables have NaN's in them, and apparently shadedplot() cannot handle that. I found that the following worked for the shading:

shadedplot(X(2:end-1),Flo(2:end-1)',Fup(2:end-1)',[.5 .5 .5],'r');

because the NaNs are in the first and last position of Flo and Fup. To make a more general solution, you would need to identify and remove the NaNs in a more general way. The isnan() command might be handy for you to do that.

on 27 Mar 2013

the cyclist, thank you very much for the help! it works fine now!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!