How to set my legend to specific multiple lines?

6 views (last 30 days)
colorspec = {[0.6350, 0.0780, 0.1840]; [0.4660, 0.6740, 0.1880] ; [0, 0.4470, 0.7410]; ...
[0.8500, 0.3250, 0.0980]; [0.9290, 0.6940, 0.1250];[0, 0.75, 0.75]; [1,0,0]; [0, 0.5, 0]; ...
[0.4940, 0.1840, 0.5560]; [0.5 0.5 0.5]}; %color
sgtitle(figure,'Significant clusters across participants'); %need 2018b version
for P = 1:Ns; %Ns = 4
subplot(2,2,P);
for ses = 1 : Nses; %Nses = 10
plot(times,squeeze(onset_res.maxt2(1,:,ses,P)),'Color', colorspec{ses},'LineWidth',1.2); %ten curves
l = line(onset_res.clustermaskneed{ses,P}(2,:),onset_res.clustermaskneed{ses,P}(1,:),'Color', colorspec{ses},'LineWidth',1.2); %ten multiple lines
hold on;
legend_str{ses} = ['Session' num2str(ses)];
end %ses
end %P
legend(l,legend_str,'location',[.9 .75 .1 .1])
My code is showed above.
I drawed 4 subplots, each contains ten curves and ten horizontal lines. Each curve corresponds to each line.
I want a legend that represents ten sessions. They should be ten different color for each session. But the legend now contains only 5 colors, I think some legend are overlapped. How can I set my legend to specific set of lines( in my case, is my curves or lines. Because they are the same sessions actually). If I name my line, for example, set 'l' to my line. There will be just one session(session ten) because of the loop.

Accepted Answer

Wenyi Xiao
Wenyi Xiao on 6 Jun 2019
l(ses)=line(onset_res.clustermaskneed{ses,P}(2,:),onset_res.clustermaskneed{ses,P}(1,:),'Color', colorspec{ses},'LineWidth',1.2);
This works.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!