How do I plot confidence intervals in MATLAB?

喜夏-厌秋 提交于 2019-12-01 08:09:19

I'm not sure what you meant by confidence intervals graph, but this is an example of how to plot a two-sided 95% CI of a normal distribution:

alpha = 0.05;          % significance level
mu = 10;               % mean
sigma = 2;             % std
cutoff1 = norminv(alpha, mu, sigma);
cutoff2 = norminv(1-alpha, mu, sigma);
x = [linspace(mu-4*sigma,cutoff1), ...
    linspace(cutoff1,cutoff2), ...
    linspace(cutoff2,mu+4*sigma)];
y = normpdf(x, mu, sigma);
plot(x,y)

xlo = [x(x<=cutoff1) cutoff1];
ylo = [y(x<=cutoff1) 0];
patch(xlo, ylo, 'b')

xhi = [cutoff2 x(x>=cutoff2)];
yhi = [0 y(x>=cutoff2)];
patch(xhi, yhi, 'b')

Ufos

After reading numerous threads, here's my attempt.

% 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

See e.g. these m-files on Matlab File Exchange:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!