Plotting legends in loop for mean and standard deviation of spectra
조회 수: 4 (최근 30일)
이전 댓글 표시
I have mean values for my spectra and their standard deviation saved as
meanC=mean(C)
stdC=std(C)
the labels for my C data are saved as labelC
wavenumbers1 is the x axis of my data.
I wand to plot the mean spectra(meanC) vs wavenumbers and I want each spectrum to have a legend. I also want to plot the meanC+std and meanC-std as a shaded area around each mean. But I want to get the legend of the standard deviation curve only one time since it's the same color for all means. How should I go about this?
at the moment since I am using a loop, I get half of the legends of the mean spectra in my plot.... should I turn off the legends for the standard deviation? how would I do that using patch?
I am open to doing this with other functions if it's more efficient that way! I'm using Matlab R2018a
Thank you!
for i=1:M;
plot(wavenumbers1,(meanC(i,:)); legend(labelC)
hold on
patch([wavenumbers1' fliplr(wavenumbers1')], [((meanC(i,:))+stdC(i,:)) fliplr((meanC(i,:))-stdC(i,:))], 'r', 'FaceAlpha',0.5, 'EdgeColor','none')
end
댓글 수: 0
채택된 답변
Matt Gaidica
2021년 1월 15일
I think what you want to do is utilize the subset input for legend. What I usually do is something like this:
lns = [];
for i=1:M
lns(i) = plot(wavenumbers1,(meanC(i,:));
hold on;
% plot other stuff
end
legend(lns,{"Label1", "Label2", "LabelM"});
댓글 수: 2
Matt Gaidica
2021년 1월 15일
Do this:
for i=1:M;
lns(i)=plot(wavenumbers1,(meanC(i,:)+i*30000));
hold on
lns(M+1)=patch([wavenumbers1' fliplr(wavenumbers1')], [((meanC(i,:)+i*30000)+stdC(i,:)) fliplr((meanC(i,:)+i*30000)-stdC(i,:))], 'r', 'FaceAlpha',0.5, 'EdgeColor','none')
end
legend(lns,{legends{:},'Standard Deviation'});
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!