How can I draw std ellipses in a scatter plot?
123 views (last 30 days)
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.
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.
% 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();
% 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]);
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
axis equal %this is important so circles appear as circles
% 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;