How can I draw 95% CI plot in my data?

61 views (last 30 days)
I have 1 data(100x1 matrix).
In Matlab, I want to draw 95% ci plot in my data
so, I found good code
like this
x = 1:100; % Create Independent Variable
y = randn(50,100); % Create Dependent Variable ‘Experiments’ Data
N = size(y,1); % Number of ‘Experiments’ In Data Set
yMean = mean(y); % Mean Of All Experiments At Each Value Of ‘x’
ySEM = std(y)/sqrt(N); % Compute ‘Standard Error Of The Mean’ Of All Experiments At Each Value Of ‘x’
CI95 = tinv([0.025 0.975], N-1); % Calculate 95% Probability Intervals Of t-Distribution
yCI95 = bsxfun(@times, ySEM, CI95(:)); % Calculate 95% Confidence Intervals Of All Experiments At Each Value Of ‘x’
figure
plot(x, yMean,'p') % Plot Mean Of All Experiments
hold on
plot(x, yCI95+yMean,'-r') % Plot 95% Confidence Intervals Of All Experiments
hold off
grid
but this graph is not what I wanted
How can i draw graph? like this..

Accepted Answer

Star Strider
Star Strider on 26 Mar 2021
Replace the second plot call with:
patch([x, fliplr(x)], [yCI95(1,:) fliplr(yCI95(2,:))], 'b', 'EdgeColor','none', 'FaceAlpha',0.25)
so the full code is now:
x = 1:100; % Create Independent Variable
y = randn(50,100); % Create Dependent Variable ‘Experiments’ Data
N = size(y,1); % Number of ‘Experiments’ In Data Set
yMean = mean(y); % Mean Of All Experiments At Each Value Of ‘x’
ySEM = std(y)/sqrt(N); % Compute ‘Standard Error Of The Mean’ Of All Experiments At Each Value Of ‘x’
CI95 = tinv([0.025 0.975], N-1); % Calculate 95% Probability Intervals Of t-Distribution
yCI95 = bsxfun(@times, ySEM, CI95(:)); % Calculate 95% Confidence Intervals Of All Experiments At Each Value Of ‘x’
figure
plot(x, yMean,'p') % Plot Mean Of All Experiments
hold on
% plot(x, yCI95+yMean,'-r') % Plot 95% Confidence Intervals Of All Experiments
patch([x, fliplr(x)], [yCI95(1,:) fliplr(yCI95(2,:))], 'b', 'EdgeColor','none', 'FaceAlpha',0.25)
hold off
grid
That should do what you want. (I appreciate your quoting my code!)
  1 Comment
Ashish Jadhav
Ashish Jadhav on 10 Apr 2022
The 'y' here is array of 'experiments'. What does this mean? How do we define 'y' in our real single vector data set?

Sign in to comment.

More Answers (0)

Categories

Find more on Time Series in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!