Adding Legend to Perfcurve

1 次查看(过去 30 天)
I am plotting "perfcurve" but class 2 is shown as a green circle in the legend. How can fix it? Thanks for the help.
for m = 1:nClasses
[FPR,TPR,~,AUC,OOP] = perfcurve(Y,score(:,m),m,'XCrit','fpr');
Legend{m} = strcat(['Class ',num2str(m),' AUC ',num2str(AUC)]);
plot(FPR,TPR,OOP(1),OOP(2),'go','LineWidth',1,'MarkerSize',6)
xlabel('FPR','FontSize',10,'FontWeight','Bold');
ylabel('TPR','FontSize',10,'FontWeight','Bold');
grid on;
hold on;
end
legend(Legend,'FontSize',8,'FontWeight','Bold','Location','Southeast')
hold off;

采纳的回答

Walter Roberson
Walter Roberson 2018-9-9
This is expected from your code. You have
plot(FPR,TPR,OOP(1),OOP(2),'go','LineWidth',1,'MarkerSize',6)
The first pair of coordinates FPR,TPR does not have a linespec after it, so it will be plotted with the default color. The second pair of coordinates OOP(1),OOP(2) has 'go' after it, so that point will be plotted as a green circle. These create two different "primitive line" objects.
You appear to have three classes, so you are executing the plot() twice. Each time you are creating two primitive line objects
When you then legend() with three strings passed in as Legend, MATLAB takes the first three graphics objects as being the ones to place the legends for -- so the first line, then the first green marker, then the line of the second class. And that is what will appear in your legend.
The solution is:
for m = 1:nClasses
[FPR,TPR,~,AUC,OOP] = perfcurve(Y,score(:,m),m,'XCrit','fpr');
Legend{m} = strcat(['Class ',num2str(m),' AUC ',num2str(AUC)]);
plothandles(:,m) = plot(FPR,TPR,OOP(1),OOP(2),'go','LineWidth',1,'MarkerSize',6);
xlabel('FPR','FontSize',10,'FontWeight','Bold');
ylabel('TPR','FontSize',10,'FontWeight','Bold');
grid on;
hold on;
end
legend(plothandles(1,:), Legend,'FontSize',8,'FontWeight','Bold','Location','Southeast')
hold off;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by