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

2 ビュー (過去 30 日間)
Nicholas
Nicholas 2022 年 10 月 7 日
コメント済み: Star Strider 2022 年 10 月 9 日
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
Star Strider 2022 年 10 月 7 日
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 件のコメント
Nicholas
Nicholas 2022 年 10 月 9 日
This worked out, thank you!
Star Strider
Star Strider 2022 年 10 月 9 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by