How can I plot shaded confidence intervals on my plot?

조회 수: 120 (최근 30일)
Sam
Sam 2020년 7월 2일
댓글: Star Strider 2021년 4월 16일
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.

채택된 답변

Star Strider
Star Strider 2020년 7월 2일
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
Eric
Eric 2021년 4월 16일
Thanks, Star Strider. This is just what I needed for my research effort...ou're the best!
Star Strider
Star Strider 2021년 4월 16일
@Eric — I very much appreciate your compliment!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by