# How do you change the color on a class in a legend?

5 views (last 30 days)
Nicholas on 7 Oct 2022
Commented: Star Strider on 9 Oct 2022
I have only included the code that does the plotting:
figure; hold on
plot(Xred(1,:),Xred(2,:),'r.');
plot(Xblue(1,:),Xblue(2,:),'b.');
title('perceptron learning');
xlabel('x1');
ylabel('x2');
for z = 1:length(t_predict)
if t_predict(z)==1
plot(X(z,1),X(z,2),'ro')
end
if t_predict(z)==-1
plot(X(z,1),X(z,2),'bo')
end
end
legend('class +1','class -1','pred +1','pred -1');
I am plotting data points with a given label and then using sequential perceptron learning algorithms for a random set of weights to predict and plot those labels over the original data points. On the legend, pred -1 should be blue but it is showing as red. Is there a way to change it blue?

Star Strider on 7 Oct 2022
Try something like this —
figure; hold on
hp{1} = plot(Xred(1,:),Xred(2,:),'r.');
hp{2} = plot(Xblue(1,:),Xblue(2,:),'b.');
title('perceptron learning');
xlabel('x1');
ylabel('x2');
for z = 1:length(t_predict)
if t_predict(z)==1
hp{3} = plot(X(z,1),X(z,2),'ro');
end
if t_predict(z)==-1
hp{4} = plot(X(z,1),X(z,2),'bo');
end
end
legend([hp{1},hp{2},hp{3}(1),hp{4}(1)],'class +1','class -1','pred +1','pred -1');
The first two appear to be plotting correctly, however the last two have problems likely related to the how they are plotted. Selecting only the first element in each of the last two should work in situations such as these, since the legend call refers to them in the order they were plotted.
If this does not work, it will be necessary to see more of your code.
.
##### 2 CommentsShowHide 1 older comment
Star Strider on 9 Oct 2022
As always, my pleasure!