How do I plot confidence intervals in MATLAB?

Haqan picture Haqan · Oct 21, 2009 · Viewed 42.2k times · Source

I want to plot some confidence interval graphs in MATLAB but I don't have any idea at all how to do it. I have the data in a .xls file.

Can someone give me a hint, or does anyone know commands for plotting CIs?

Answer

Ufos picture Ufos · Apr 1, 2016

After reading numerous threads, here's my attempt.

enter image description here

% Get some random data
x       = linspace(0.3, pi-0.3, 10);
Data    = sin(x) + randn(1, 10)/10;
Data_sd = 0.1+randn(1,10)/30;

% prepare it for the fill function
x_ax    = 1:10;
X_plot  = [x_ax, fliplr(x_ax)];
Y_plot  = [Data-1.96.*Data_sd, fliplr(Data+1.96.*Data_sd)];

% plot a line + confidence bands
hold on 
plot(x_ax, Data, 'blue', 'LineWidth', 1.2)
fill(X_plot, Y_plot , 1,....
        'facecolor','blue', ...
        'edgecolor','none', ...
        'facealpha', 0.3);
hold off 

Mostly based on this question: Plotting with transparency