Add number of data points to gscatter() legend

조회 수: 8 (최근 30일)
newbie9
newbie9 2018년 4월 1일
답변: Alok Nimrani 2018년 4월 6일
I have a set of data points I am plotting using gscatter(). All the points classified as "Type1" plot as blue circles, "Type2" plot as green circles, etc. I'd like the legend to also show the number of points in each dataset (e.g., 'Type1, n=7'). These data points are in the "screened_data" table in the code below. There are other things that I'm also plotting that I don't want in the legend (i.e., I don't want "data2" or the line plot to be in the legend). Thanks in advance for any clever (or obvious) ideas!
data2 = scatter(selected.x, selected.y, 300, 'pentagram', 'MarkerEdgeColor', 'black', 'MarkerFaceColor', 'white');
n = max(screened_data.nTypes);
C = hsv(n);
data1 = gscatter(screened_data.x, screened_data.y, screened_data.Type, C);
xmin = 3;
xmax = 300;
ymin = 8;
ymax = 200;
xlim([xmin xmax])
ylim([ymin ymax])
plot([xmin xmax], [32 32], 'Color', 'black')
text(xmin+0.1, 31.5, 'Lable for 32', 'FontSize', 6);
lgnd = legend([data1], 'Location', 'eastoutside');
lgnd.FontSize = 8;

채택된 답변

Alok Nimrani
Alok Nimrani 2018년 4월 6일
Hi Alex,
‘legend’ can take the subset of graphics objects as its argument:
>> legend(subset, _);
This will include only the subset objects in the legend. Since, in your code, you are passing ‘data1’ as an argument to ‘legend’, the legend should display only the types related to ‘data1’ i.e. the values in ‘screened_data.Type’.
So, I am not understanding what you mean when you say that you do not want ‘data2’ or the line plot in the legend. Could you please elaborate on that or share the figure the is getting displayed when you execute your code?
Regarding the need to show the number of points of each dataset in the legend text, you can try setting the ‘DisplayName’ property as per your need. The text that is displayed in the legend is the value of ‘DisplayName’ property for each element. Below is the modified code that handles this requirement:
load carsmall % sample dataset
hold on;
% sample x and y values
data2 = scatter(Displacement, Horsepower, 50, 'pentagram', 'MarkerEdgeColor', 'black', 'MarkerFaceColor', 'white', 'DisplayName', 'Data2');
n = 6; % sample n
C = hsv(n);
data1 = gscatter(Weight, MPG, Origin, C); % x = Weight, y = MPG and Type = Origin
for i = 1:numel(data1)
data1(i).DisplayName = strcat(data1(i).DisplayName, ', n= ', string(numel(data1(i).XData)));
end
% sample values for max, min, plot and text
xmin = 00;
xmax = 4000;
ymin = 5;
ymax = 200;
xlim([xmin xmax])
ylim([ymin ymax])
p = plot([xmin xmax], [10 10], 'Color', 'black', 'DisplayName', 'Data3');
t = text(xmin+20, 15, 'Label for 10', 'FontSize', 9);
lgnd = legend(data1, 'Location', 'eastoutside');
lgnd.FontSize = 8;
hold off;
The above code results in the following figure:
For more information about ‘legend’ and the ‘DisplayName’ property, you can check the following link: https://www.mathworks.com/help/matlab/ref/legend.html
Hope this helps.
Thanks.

추가 답변 (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