Shapes in MATLAB®

How to plot bais Shapes in MATLAB® with Plotly.


Plotly is known for its graphing libraries, but it's also a broader ecosystem for building interactive data apps, AI data analysis tools, and deploying and hosting Dash apps. Explore the full set of tools and platforms designed to help turn data into action. Discover Plotly.

Note: We are retiring documentation for R, MATLAB, Julia, and F#. Learn more about this change here.

Circle

r = 10;
fig = figure;
hold on

th = 0:pi/100:2*pi;
xunit = r * cos(th);
yunit = r * sin(th);
plot(xunit, yunit);

hold off
axis square;

fig2plotly(gcf);

Plot A Unit Circle in Complex Plane

theta = 0:pi/100:2*pi;
r = 1;
z = r*cos(theta) + i*r*sin(theta);

fig = figure;
plot(z);

fig2plotly(gcf);

Ellipse

t=0:0.1:2*pi;
x=2*cos(t);
y=3*sin(t);
plot(x,y);
grid on;

fig2plotly(gcf);

Covariance Ellipse

num = 50;
X = [ mvnrnd([0.5 1.5], [0.025 0.03 ; 0.03 0.16], num) ; ...
      mvnrnd([1 1], [0.09 -0.01 ; -0.01 0.08], num)   ];
G = [1*ones(num,1) ; 2*ones(num,1)];
fig = figure;

gscatter(X(:,1), X(:,2), G)
axis equal, hold on

for k=1:2
    idx = ( G == k );

    Mu = mean( X(idx,:) );
    X0 = bsxfun(@minus, X(idx,:), Mu);

    [V D] = eig( X0'*X0 ./ (sum(idx)-1) );  
    [D order] = sort(diag(D), 'descend');
    D = diag(D);
    V = V(:, order);

    t = linspace(0,2*pi,100);
    e = [cos(t) ; sin(t)];        
    VV = V*sqrt(D);             
    e = bsxfun(@plus, VV*e, Mu');

    plot(e(1,:), e(2,:), 'Color','k');

end

fig2plotly(gcf);