# How can I draw std ellipses in a scatter plot?

123 views (last 30 days)
Ifigenia Aslanidou on 20 Aug 2019
Commented: Adam Danz on 23 Aug 2019
Hello everyone!!
I have a question regarding, drawing an std ellipse at the scatter plot where the center of the ellipse is the mean of my data.
So in the attachment you can see an example of what I am trying to achieve(below) and my scatter plot(above).
My scatter plot is made through reading in in every loop a group of data. So I have 6 Measurements.
The dots represent the mean of the results of each loop, and the x are the results. (x and y axes: Turbulence intensities of u and v velocity each [%])
I appreciate any help :)

Adam Danz on 20 Aug 2019
Edited: Adam Danz on 23 Aug 2019
Here's a function that will plot std ellipses where the vertical distance to the center is 1 std of y and the horizontal distance to the center is 1 std for x.
% cnt is the [x,y] coordinate of the center (row or column index).
% rads is the [horizontal, vertical] "radius" of the ellipses (row or column index).
% axh is the axis handle (if missing or empty, gca will be used)
% h is the object handle to the plotted rectangle.
% The easiest approach IMO is to plot a rectangle with rounded edges.
% EXAMPLE
% center = [1, 2]; %[x,y] center (mean)
% stdev = [1.2, 0.5]; %[x,y] standard dev.
% h = plotEllipses(center, stdev)
% axis equal
% get axis handle if needed
if nargin < 3 || isempty(axh)
axh = gca();
end
% Compute the lower, left corner of the rectangle.
% Compute the width and height
% Draw rectangle
h = rectangle(axh,'Position',[llc(:).',wh(:).'],'Curvature',[1,1]);
end
Here's a demo that plots random dots from two different distributions and adds colored std ellipses.
% Create data
d1 = randn(2,500)+1;
d2 = (randn(2,500)-2) .*[1;2]; %greater spread along y-axis.
% Compute mean and std
mean1 = mean(d1,2);
std1 = std(d1,[],2);
mean2 = mean(d2,2);
std2 = std(d2,[],2);
% Plot data
figure()
plot(d1(1,:),d1(2,:),'b.')
hold on
plot(d2(1,:),d2(2,:),'r.')
axis equal %this is important so circles appear as circles
grid on
% Plot ellipses, then change their color and other properties
h = plotEllipses(mean1,std1);
h.FaceColor = [0 0 1 .3]; %4th value is undocumented: transparency
h.EdgeColor = 'b';
h.LineWidth = 2;
h = plotEllipses(mean2,std2);
h.FaceColor = [1 0 0 .3]; %4th value is undocumented: transparency
h.EdgeColor = 'r';
h.LineWidth = 2;
##### 2 CommentsShowHide 1 older comment
Adam Danz on 23 Aug 2019
Thanks for the link! What's nice about the function you shared is that it can also show the error bounds along a regression line. The function in my answer was only designed to show error along the x and y axes.