Question about the legend command
2 次查看(过去 30 天)
显示 更早的评论
Hi, I am working on the following code to plot three different classes:
% a - Plot the data in a two-dimensional vector space.
clc;clear;
% Class 1
x1 = [16; 18; 20; 11; 17; 8; 14; 10; 4; 7;];
y1 = [13; 13; 13; 12; 12; 11; 11; 10; 9; 9];
class1 = [x1 y1];
% Class 2
x2 = [8; 9; 6; 8; 5; 7; 4; 6; 4; 3];
y2 = [8; 7; 7; 6; 5; 5; 4; 3; 2; 2];
class2 = [x2 y2];
% Class 3
x3 = [19; 19; 17; 17; 16; 14; 13; 13; 11; 11];
y3 = [6; 3; 8; 1; 4; 5; 8; 1; 6; 3];
class3 = [x3 y3];
figure
gscatter(x1,y1,class1,'b')
xlabel('Band 1'), ylabel('Band 2')
title('Data of Each Class in Two-Dimensional Vector Space')
hold on
gscatter(x2,y2,class2,'r','x')
hold on
gscatter(x3,y3,class3,'g','o')
legend('Class 1', 'Class 2', 'Class 3')
I am getting what I want on the plot but the legend says Class 1, Class 2, Class 3 but it shows the point structure from class 1 on each of the classes. What can I do on the legend to show the points of each class, respectively?
0 个评论
采纳的回答
Walter Roberson
2016-2-3
You cannot do that. gscatter() creates one line for every group, and you have defined each point to be part of different groups, so each of your gscatter() is going to produce 10 lines. You are then trying to legend() those 30 lines total with only 3 legends.
I think you need to go back and re-read the requirements for the grouping variable; see http://www.mathworks.com/help/stats/grouping-variables.html
You would need your grouping variable to be all the same in order to have only one line per plot produced.
0 个评论
更多回答(2 个)
Brendan Hamm
2016-2-3
The problem is not with the legend. You can note that if you keep adding new legend entries, you will get more markers shown:
legend('Class 1', 'Class 2', 'Class 3','A','B','C','D','E','F','G','H','I','J','K')
This is because you have way more groups than what you think you are creating.
gscatter(x,y,group) is meant to scatter the points formed by the x-y pairs and color (or change markers of) the groups based on the grouping variable group. When you pass the matrix class1 as the grouping variable it is making a unique group for each unique row in the matrix, which happens to be every row.
To use this correctly, consider the example:
x = (1:10)';
y = (1:10)';
grp = [ones(5,1);zeros(5,1)]
gscatter(x,y,grp) % First 5 pairs are of group1 and next 5 pairs are group0.
It seems likely that what you are looking for is:
x = [x1;x2;x3];
y = [y1;y2;y3];
% Grouping Variable is the class number for each observation in x and y
grp = [ones(size(x1));2*ones(size(x2));3*ones(size(x3))];
gscatter(x,y,grp,'brg','.xo')
Now legend will do what you expect.
dpb
2016-2-3
编辑:dpb
2016-2-3
Since it really isn't content of the x,y that you're interested in grouping on, create the grouping variable as (or similarly)--
class=[ones(size(x1));2*ones(size(x2));3*ones(size(x3))];
gscatter([x1;x2;x3],[y1;y2;y3],class,'brg','.xo')
legend will subsequently work as expected/desired as well...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!