How can I plot shaded confidence intervals on my plot?

108 views (last 30 days)
I am trying to plot the confidence intervals for males and females on my graph. I have been using simple line plots for the female and male data and their upper and lower confidence intervals, using the following code
x=[5,610,1115,1620,2125,2630,3135,3640,4145,4650];
y=[1.5728,1.6011,1.5828,1.583,1.5779,1.5931,1.6136,1.6132,1.6354,1.6512];
CI = [0.047204964 0.020555517 0.030005595 0.023338819 0.030057518 0.026567109 0.027295407 0.025855224 0.029278635 0.028793178]; %CI values
y2 = [1.4904,1.625,1.5834,1.6213,1.6135,1.6378,1.6212,1.6563,1.662,1.6657,1.6462,1.7267]
x2 = [5,610,1115,1620,2125,2630,3135,3640,4145,4650, 5155, 5660]
CI2 = [0,0.02859404,0.03932538,0.031767406, 0.033377728,0.033181883,0.02925182, 0.030260765,0.028698134,0.026206678,0.035101913,0]
plot(x, y, 'color', [0, .6, .77])
hold on
plot(x, y-CI, ':', 'color', [0, .6, .77])
hold on
plot(x , y+CI, ':','color', [0, .6, .77])
hold on
plot(x2, y2, 'color', [.89, 0, .23])
hold on
plot(x2, y2-CI2, ':', 'color', [.89, 0, .23])
hold on
plot(x2 , y2+CI2, ':','color', [.89, 0, .23])
hold off
ylabel('vTMD')
xlabel('Depth (%)')
xticks([5, 610, 1115, 1620, 2125, 2630, 3135, 3640, 4145, 4650, 5155, 5660])
xticklabels({'0-5', '6-10', '11-15', '16-20', '21-25', '26-30', '31-35', '36-40', '41-45', '46-50', '51-55', '56-60'})
This gives me a graph that looks like this -
Is there a way to shade the individual CI as it is pretty hard to read right now! For example, a lighter blue in between the dashed blue lines.

Accepted Answer

Star Strider
Star Strider on 2 Jul 2020
Add these two patch calls after the last plot call and before the hold off call:
patch([x fliplr(x)], [y-CI fliplr(y+CI)], [0, .6, .77], 'FaceAlpha',0.5, 'EdgeColor','none')
patch([x2 fliplr(x2)], [y2-CI2 fliplr(y2+CI2)], [.89, 0, .23], 'FaceAlpha',0.5, 'EdgeColor','none')
They use the appropriate colours for the patch objects. Experiment with them to get different results.
  4 Comments
Eric
Eric on 16 Apr 2021
Thanks, Star Strider. This is just what I needed for my research effort...ou're the best!

Sign in to comment.

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!