Cannot add shaded confidence intervals?

7 views (last 30 days)
Hello all,
I have a graph with confidence intervals, where I want to make it so that the confidence intervals are shaded between both of them. I have the following code:
% 1) getting confidence intervals
[ci_groups, ci_pre, ci_post]=bootstrapping_function(matrix_mean, matrix_post_mean)
% 2) taking confidence intervals and multipling by size of matrix
ci_lower_pre = ci_pre(1)*ones(size(matrix_mean));
ci_higher_pre = ci_pre(2)*ones(size(matrix_mean));
% 3) making figure
plot(f_mr,matrix_mean, '-b'); hold on
% 4) fill the area between the confidence intervals
x = [f_mr, fliplr(f_mr)];
y = [matrix_mean + ci_higher_pre, fliplr(matrix_mean - ci_lower_pre)];
fill(x, y, 'r', 'FaceAlpha', 0.5, 'EdgeColor', 'none');
But when I use the code above I get the attached 'example' as my figure, where there is shading outside of the confidence intervals. Do you have any advice?
Thanks a ton

Accepted Answer

Star Strider
Star Strider on 31 Jan 2023
We do not have your data. The fliplr function will not produce the desired result with column data.
I prefer to use the patch function —
f_mr = 1:10;
matrix_mean = rand(size(f_mr));
ci_higher_pre = rand(size(f_mr))/5;
ci_lower_pre = rand(size(f_mr))/5;
x = [f_mr, flip(f_mr)];
y = [matrix_mean + ci_higher_pre, flip(matrix_mean - ci_lower_pre)];
patch(x, y, 'r', 'FaceAlpha', 0.5, 'EdgeColor', 'none') % Row Vectors
f_mr = f_mr(:);
matrix_mean = matrix_mean(:);
ci_higher_pre = ci_higher_pre(:);
ci_lower_pre = ci_lower_pre(:);
x = [f_mr; flip(f_mr)];
y = [matrix_mean + ci_higher_pre; flip(matrix_mean - ci_lower_pre)];
patch(x, y, 'r', 'FaceAlpha', 0.5, 'EdgeColor', 'none') % Column Vectors

More Answers (0)


Find more on Specifying Target for Graphics Output 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!