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

1 Ansicht (letzte 30 Tage)
Nicholas
Nicholas am 7 Okt. 2022
Kommentiert: Star Strider am 9 Okt. 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?

Akzeptierte Antwort

Star Strider
Star Strider am 7 Okt. 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.
.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by