Why is the legend in my MATLAB code showing only one color for all the items, even though the colors are different in the parallel coordinates graph?

3 views (last 30 days)
% Define colors for each alphaclass
colors = {[1 0 0], [0 1 0], [0 0 1], [1 0 1]}; % RGB values for red, green, blue, magenta
alphaClasses = repelem(1:4, numImages/4);
% Create a parallel coordinates plot for the projected images
figure;
for i = 1:length(alphaClasses)
line(1:numComponents, projectedImages(i,:), 'Color', colors{alphaClasses(i)});
end
xlabel('AB');
ylabel('CD');
title('Parallel Coordinates Plot of Projected Images');
legend('ab', 'cd', 'ef', 'gh', 'Location', 'northeast');

Answers (1)

Dyuman Joshi
Dyuman Joshi on 16 Apr 2023
Because there is only one line in the plot, as the plot is getting overwritten in each iteration. You need to use the command
"hold on" to retain all the plots in the same figure.
figure
hold on
for i = 1:length(alphaClasses)
line(1:numComponents, projectedImages(i,:), 'Color', colors{alphaClasses(i)});
end
%use hold off after the loop if necessary

Products

Community Treasure Hunt

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

Start Hunting!