How can I draw std ellipses in a scatter plot?
120 ビュー (過去 30 日間)
古いコメントを表示
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 :)
0 件のコメント
採用された回答
Adam Danz
2019 年 8 月 20 日
編集済み: Adam Danz
2019 年 8 月 23 日
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;
2 件のコメント
Adam Danz
2019 年 8 月 23 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Scatter Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!