Legend doesn't show correct colors

2 views (last 30 days)
hi, i'm trying to plot my erp and the legend doesn't show the correct colors.
the code:
figure;
for i_chan = 4 %here specify the index of the channel
shadedErrorBar(EEG_EC_epochrej.times, EKP_avg_EC(i_chan,:), (SD_EC(i_chan,:)/sqrt(nsamples)), 'g',1);
hold on
shadedErrorBar(EEG_EC_epochrej.times, EKP_avg_EO(i_chan,:), (SD_EO(i_chan,:)/sqrt(nsamples)), 'b',1);
shadedErrorBar(EEG_EC_epochrej.times, EKP_avg_nonREM(i_chan,:), (SD_nonREM(i_chan,:)/sqrt(nsamples)), 'm',1);
shadedErrorBar(EEG_EC_epochrej.times, EKP_avg_REM(i_chan,:), (SD_REM(i_chan,:)/sqrt(nsamples)), 'r',1);
legend({'EC', 'EO', 'nonREM', 'REM'}, 'Location', 'southwest')
xlabel('Time [ms]')
ylabel('Amp [muV]');
xlim([-100 650]) %adjust limit based on your epoch length
title(EEG_EC_epochrej.chanlocs(chan(i_chan)).labels)
end
Thanks for the help

Accepted Answer

Walter Roberson
Walter Roberson on 19 Apr 2021
Your code assumes that shadedErrorBar creates exactly one graphics object per call. However if you are using https://www.mathworks.com/matlabcentral/fileexchange/26311-raacampbell-shadederrorbar then shadedErrorBar creates:
  • one line for each column of y (or one line if y is a row vector)
  • one patch object for the shading
  • two edge lines
For a total of 4 graphics objects for your case (where you are passing in one row per call)
shadedErrorBar returns a struct with three fields. If you want the lines to be marked in the legend, then extract the field named mainline from the return from each of the calls. If you want the shading to be marked in the legend, then extract the field named patch from the return from each of the calls. Then pass the vector of graphic handles as the first paramter to legend(), before the legend strings.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!