Clear Filters
Clear Filters

How to plot the sample mean in a scatter plot?

12 views (last 30 days)
Hi; Yesterday I had a question about the gscatter command and with the help I got here, I got the results that I wanted:
% 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(1)
x = [x1;x2;x3];
y = [y1;y2;y3];
grp = [ones(size(x1));2*ones(size(x2));3*ones(size(x3))]; % Grouping variableis the class number for each observation in Band 1 and Band 2
gscatter(x,y,grp,'brg','.xo'), xlabel('Band 1'), ylabel('Band 2'), title('Data of Each Class in a Two-Dimensional Vector Space'), legend('Class 1', 'Class 2', 'Class 3')
Now, I need to plot the sample mean of each class on the plot but I can't seem to find what I'm doing wrong. The code is as follows:
% b - Compute the sample means and sample covariance matrices for each class and plot the means in the same vector space with the data
% Class 1
M1 = mean(class1) % Sample Mean of Class 1
C1 = cov(class1) % Covariance Matrix of Class 1
% Class 2
M2 = mean(class2) % Sample Mean of Class 2
C2 = cov(class2) % Covariance Matrix of Class 2
% Class 3
M3 = mean(class3) % Sample Mean of Class 3
C3 = cov(class3) % Covariance Matrix of Class 3
figure(2)
x = [x1;x2;x3];
y = [y1;y2;y3];
grp = [ones(size(x1));2*ones(size(x2));3*ones(size(x3))]; % Grouping variableis the class number for each observation in Band 1 and Band 2
gscatter(x,y,grp,'brg','.xo'), xlabel('Band 1'), ylabel('Band 2'), title('Data of Each Class in a Two-Dimensional Vector Space with the Sample Means')
hold on
plot(M1(1,1),M1(1,2),'b')
hold on
plot(M2(1,1),M2(1,2),'r')
hold on
plot(M3(1,1),M3(1,2),'g'),legend('Class 1','Class 2','Class 3','Mean_Class 1','Mean_Class 2','Mean_Class 3')
The mean of each class is not shown in the figure. How can I plot the sample mean of each class?

Accepted Answer

Image Analyst
Image Analyst on 4 Feb 2016
Edited: Image Analyst on 4 Feb 2016
You didn't make an appropriate marker. Make the last chunk of your code this, and it will do it.
figure(2)
x = [x1;x2;x3];
y = [y1;y2;y3];
grp = [ones(size(x1));2*ones(size(x2));3*ones(size(x3))]; % Grouping variableis the class number for each observation in Band 1 and Band 2
gscatter(x,y,grp,'brg','.xo');
xlabel('Band 1');
ylabel('Band 2');
title('Data of Each Class in a Two-Dimensional Vector Space with the Sample Means', 'FontSize', 24)
hold on
plot(M1(1,1),M1(1,2), 'b*', 'MarkerSize', 18, 'LineWidth', 2)
plot(M2(1,1),M2(1,2), 'r*', 'MarkerSize', 18, 'LineWidth', 2)
plot(M3(1,1),M3(1,2), 'g*', 'MarkerSize', 18, 'LineWidth', 2)
legend('Class 1','Class 2','Class 3','Mean of Class 1','Mean of Class 2','Mean of Class 3')
grid on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by Augustin', 'NumberTitle', 'Off')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!