manipulating plot legend, assigning colours
Show older comments
Hello,
I have complex data which I want to plot. My code is:
if true
% code
end
for bb = 1:size(Data.Measured,2)
%calculating how many measurements we have for right and left ear
idx1 = strfind(Data.Side, 'Right');
idx1 = find(not(cellfun('isempty',idx1)));
N (bb) = length(idx1);
idx2 = strfind(Data.Side, 'Left');
idx2 = find(not(cellfun('isempty',idx2))) ;
M (bb) = length(idx2) ;
for kk = 1:size(Data.Name,2)
if strcmpi(Data.Side{kk}, 'Right')
subplot(2,1,1)
Data.legend.sub1{c1} = regexprep(Data.Name{kk},'_',' ');
c1 = c1+1;
legend(Data.legend.sub1(1:N))
title([name(bb),' Right'])
else
subplot(2,1,2)
title([name(bb),' Left'])
Data.legend.sub2{c2} = regexprep(Data.Name{kk},'_',' ');
c2 = c2+1;
legend(Data.legend.sub2(N+1:N+M))
end
end
the problem I have here is that my Data.legend.sub1 and Data.legend.sub2 give me all measurements done for all subjects but in reverse order:
Data.legend.sub1

Data.legend.sub2

Thus, my legend is using different colours for different measurements. First, I want to make sure that I am only plotting measurements for right side (is it already ensured by line :if strcmpi(Data.Side{kk}, 'Right')?). second I want to use the same colours for the same measurement for both sides. third, I am not sure why I am getting warning 'Ignoring extra legend entries.' and then one measurement is always omitted in the legend and specified just as 'data 1'...

I hope my explanation is clear.
8 Comments
Kevin Chng
on 16 Oct 2018
Edited: Kevin Chng
on 16 Oct 2018
Try to put those legend outside the for loop
for kk = 1:size(Data.Name,2)
if strcmpi(Data.Side{kk}, 'Right')
a=subplot(2,1,1)
Data.legend.sub1{c1} = regexprep(Data.Name{kk},'_',' ');
c1 = c1+1;
title([name(bb),' Right'])
else
b=subplot(2,1,2)
title([name(bb),' Left'])
Data.legend.sub2{c2} = regexprep(Data.Name{kk},'_',' ');
c2 = c2+1;
end
end
legend(a,Data.legend.sub1(1:N))
legend(b,Data.legend.sub2(N+1:N+M))
'Ignoring extra legend entries.' means your legend labelling more than the lines.
In the same time, i suspect there is empty cell in Data.legend.sub1 and Data.legend.sub2.
Do you mind to display those variables?
display(a,Data.legend.sub1)
display(b,Data.legend.sub2)
and post the display result here.
KDRA
on 16 Oct 2018
Kevin Chng
on 16 Oct 2018
Do you mind to display those variables?
display(a,Data.legend.sub1)
display(b,Data.legend.sub2)
and post the display result here. I want to see why there is data1.
KDRA
on 16 Oct 2018
@KDRA,
Did you even try any of the methods given in my answer? It's really simple, the plot in itself is not complex at all.
Make it simple for people to help you. You have posted only parts of the code (there is no plot call?)... and obviously key information is missing.
Adam
on 16 Oct 2018
With regard to the part of the question in the title (which appears to be only a small part!) you shouldn't be attempting to manipulate colours in a legend. The legend takes the colours from the plots it is referring to. Change the plot colours if they are not as you wish, and the legend will reflect this, but you shouldn't be independently trying to change colours in the legend itself.
KDRA
on 16 Oct 2018
Adam
on 16 Oct 2018
The colours in the legend should still reflect the colours of the lines though otherwise what is the point of the legend?
Accepted Answer
More Answers (0)
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!