How can I draw std ellipses in a scatter plot?

123 views (last 30 days)
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 [%])
scatter IA.PNG
Scatter_ellipse.PNG
I appreciate any help :)

Accepted Answer

Adam Danz
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.
function h = plotEllipses(cnt,rads,axh)
% 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.
llc = cnt(:)-rads(:);
% Compute the width and height
wh = rads(:)*2;
% 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;
190823 071759-Figure 1.jpg
  2 Comments
Ifigenia Aslanidou
Ifigenia Aslanidou on 23 Aug 2019
Thanks for your help :)
I also found online another script for the covariance ellipse (Gaussian distribution-95%) which also worked for me, if you want to take a look.
Here is the link and in the attachment is the code.
Adam Danz
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!