Add number of data points to gscatter() legend
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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;
0 Commenti
Risposta accettata
Alok Nimrani
il 6 Apr 2018
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Legend in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!