Problem with figure legend in loop for selecting multiple columns
Show older comments
I am having difficulty with my legend displaying the correct colors for a loop plotting figure.
To trouble shoot, I have tried matrices and cell formats for my x and y variables. Both x and y contain 6 sets of 70x70 matrices. (So, for the matrix format, it becomes a 70x420 matrix)
Here the code works with the legend (where x and y are a 70x70 matrix)
N=6;
figure
hold on
clr = jet(N);
for j=1:N;
plot(x(1:end,j), y(1:end,j), 'Color',clr(j,:))
end
hold off
str = cellstr( num2str((1:N)','Run%d') );%
legend(str)
This creates a figure that displays different colors on the legend corresponding to the plot colors correctly

However, when I try to select various sets of columns in my 70x70 matrix in the x and y matrix, the legend colors become corrupt and display 4 blues and 2 cyan:
N=6;
figure
hold on
clr = jet(N);
for j=1:N;
plot(x(1:end,j:j+1), y(1:end,j:j+1), 'Color',clr(j,:))
end
hold off
str = cellstr( num2str((1:N)','Run%d') );%
legend(str)

Does anyone know how to fix the second image to correlate plot and legend colors? Why is it only when I try to select various columns?
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!