Matlab plot cheatsheet

Ming Sun

Ming Sun / October 26, 2022

15 min read––– views

Basic 2D plot

% Illustrating 2D plot

clear;clc;close all;

% Use 'plot' command to generate 2D plot
x = linspace(0, 4*pi, 10); 
y = x.^2.*sin(x);

% start a brand new feature
figure;
plot(x, y, 'm-', 'LineWidth', 3);
hold on;
plot(x, y, "m^", 'MarkerSize', 14, 'LineWidth',3);
xlabel('x (seconds)');
ylabel('y = x^2*sin(x)');
grid on;
title('x vs. t, an example plot');
legend('y', 'y (data points)', 'Location','southwest');
axis([min(x), max(x) min(y) max(y)])
text(2, -40, 'The angle of the wheel \theta', 'Color', 'red', 'FontSize',16);
Basic 2D plot
Fig. 1Basic 2D plot[1]

Another example

clc; clear; close all;

Vin = 5;
D = 0.5;
L = 1e-6;
C = 1e-6;
R = 1;
Tsw = 1e-6;
Dp = 1 - D;

N = 100;
dt =Tsw/N;

V = D*Vin;
Iave = V/R;
Sr = (Vin-V)/L;
Sf = -V/L;

Imin = Iave - Sr*0.5*D*Tsw;
Imax = Iave + Sr*0.5*D*Tsw;

V0 = 2.423;
V2 = V0;
Vo_min = V0 - V*D*Dp*Tsw^2/8/L/C;
Vo_max = V0 + V*Dp^2*Tsw^2/8/L/C;

k=1;
for i=0:dt:D*Tsw
    t(k) = i*1e6;
    I(k) = Imin + Sr*i;
    Vo(k) = V0 - V*Dp*Tsw/2/L/C*i + Sr*i^2/2/C;
    k = k+1;
end

for i=D*Tsw:dt:Tsw
    t(k) = i*1e6;
    I(k) = Imax + Sf*(i-D*Tsw);
    Vo(k) = V2 + V*Dp*Tsw/2/L/C*(i-D*Tsw)+Sf/2/C*(i-D*Tsw)^2;
    k = k+1;
end

for i=Tsw:dt:Tsw+D*Tsw
    t(k) = i*1e6;
    I(k) = Imin + Sr*(i-Tsw);
    Vo(k) = V0 - V*Dp*Tsw/2/L/C*(i-Tsw) + Sr*(i-Tsw)^2/2/C;
    k = k+1;
end

for i=Tsw+D*Tsw:dt:2*Tsw
    t(k) = i*1e6;
    I(k) = Imax + Sf*(i-Tsw-D*Tsw);
    Vo(k) = V2 + V*Dp*Tsw/2/L/C*(i-Tsw-D*Tsw)+Sf/2/C*(i-Tsw-D*Tsw)^2;
    k = k+1;
end

yyaxis('left');
plot(t,I, "LineWidth",2,'Color', '#0284C7');
hold on;
plot([0,2*Tsw*1e6],[Iave,Iave], '--', "LineWidth",1);
% text(0.05e-6, Iave+0.06, '$I_{ave}$ ', 'Interpreter','latex', 'Color', '#0284C7', 'FontSize',16);
plot([D*Tsw*1e6,D*Tsw*1e6],[Imin,Imax], '-.', "LineWidth",1);
plot([Tsw*1e6+D*Tsw*1e6,Tsw*1e6+D*Tsw*1e6],[Imin,Imax], '-.', "LineWidth",1);
% text(0.52e-6, 2.1, '$DT_{S}$ ', 'Interpreter','latex', 'Color', '#0284C7', 'FontSize',16);
plot([0.5*D*Tsw*1e6,0.5*D*Tsw*1e6],[Imin,Imax], ':', "LineWidth",1);
plot([Tsw*1e6+0.5*D*Tsw*1e6,Tsw*1e6+0.5*D*Tsw*1e6],[Imin,Imax], ':', "LineWidth",1);
% text(0.26e-6, 2.1, '$\frac{DT_S}{2}$ ', 'Interpreter','latex', 'Color', '#0284C7', 'FontSize',20);
plot([D*Tsw*1e6+0.5*Dp*Tsw*1e6,D*Tsw*1e6+0.5*Dp*Tsw*1e6],[Imin,Imax], ':', "LineWidth",1);
plot([Tsw*1e6+D*Tsw*1e6+0.5*Dp*Tsw*1e6,Tsw*1e6+D*Tsw*1e6+0.5*Dp*Tsw*1e6],[Imin,Imax], ':', "LineWidth",1);
grid on;
ylim([Imin, Imax]);
ylabel("Inductor current [A]");

yyaxis('right');
plot(t,Vo, "LineWidth",2);
grid on;
ylim([Vo_min, Vo_max]);
ylabel("Output voltage [V]");

xlabel('Time [µs]')
Inductor current and output voltage
Fig. 2Inductor current and output voltage[1]

Histrogram

% histogram
N = 2000;
NumBins = 20;

samplesUniform = rand(1, N);
samplesNormal = randn(1, N);

figure;
subplot(2,1,1);
histogram(samplesUniform, NumBins);

subplot(2,1,2);
histogram(samplesNormal, NumBins);
Histrogram
Fig. 3Histrogram[1]

yyaxis

% yyaxis
x2 = linspace(0, 5*pi, 20);
y2 = x2.^3.*sin(x2);

figure
hold on;
yyaxis('left');
plot(x, y, 'r', 'LineWidth', 2);
yyaxis('right');
plot(x2,y2, 'b--');
grid on;
yyaxis
Fig. 4yyaxis[1]

semilogx

% semilogx
figure;
semilogx(x2, y2);
grid on;
semilogx
Fig. 5semilogx[1]

Pie chart

sales = [15 50 30 30 20];

figure
pie(sales);
Pie chart
Fig. 6Pie chart[1]

Scatter plot

figure;
subplot(2,1,1);
scatter(x,y);
grid on;

subplot(2,1,2);
plot(x, y, 'b+')
grid on;
Scatter plot
Fig. 7Scatter plot[1]

Plot3

%% 3D lines
% plot3
% x = [1 2 5];
% y = [2 3 4];
% z = [1 3 0];

t = linspace(0, 6*pi, 30);
 x = 3*cos(t);
 y = 1*sin(t);
 z = 0.01*t.^2;

figure;
hold on;
plot3(x,y,z);
plot3(x,y,z, 'mo');
xlabel('x')
ylabel('y')
zlabel('z')
grid on;
axis('equal')

Plot3
Fig. 8Plot3[3]

Mesh and Meshgrid

%% 3D surfaces
%  patch
x = [1 2 5];
y = [2 3 4];
z = [1 3 0];

figure
patch(x, y, z, 'm');
grid on;

% mesh
% x1 = [-1 1];
% x2 = [-2 -0.1 1.7];
x1 = linspace(-pi, pi, 20);
x2 = linspace(-10, 16, 30);

%  call meshgrid to create a cartesian product of x1, x2
[X1, X2] = meshgrid(x1, x2);

% Evaluate the function at these (X1, X2) pairs
Z = cos(X1).*X2;
mesh(X1, X2, Z);
grid on;
xlabel("x_1")
ylabel("x_2")
zlabel('z = f(x_1, x_2)')
title('Using the ''mesh'' function')
view([35 30])

Mesh and Meshgrid
Fig. 9Mesh and Meshgrid[3]

Surface plot

% surf
figure
surf(X1, X2, Z);
grid on;
xlabel("x_1")
ylabel("x_2")
zlabel('z = f(x_1, x_2)')
title('Using the ''surf'' function')
view([35 30])
% get rid of black lines in the surface plot
shading interp   
colormap(jet(50))
colorbar   % adds a colorbar that acts as a legend for colors
Surface plot
Fig. 10Surface plot[3]

Contour plot

% contour
figure
% contour(X1, X2, Z);
contourf(X1, X2, Z);
grid on;
xlabel("x_1")
ylabel("x_2")
zlabel('z = f(x_1, x_2)')
title('Using the ''contour'' function')
Contour plot
Fig. 11Contour plot[3]

Surface + Contour plot

% surfc
figure
surfc(X1, X2, Z);
grid on;
xlabel("x_1")
ylabel("x_2")
zlabel('z = f(x_1, x_2)')
title('Using the ''surf'' function')
view([35 30])
% get rid of black lines in the surface plot
shading interp   
colormap(jet(50))
colorbar   % adds a colorbar that acts as a legend for colors
Surface + Contour plot
Fig. 12Surface + Contour plot[3]

Surface + plot3

% Make a composite plot
x1_line = linspace(-1,1, 20);
x2_line = linspace(0,10,20);
z_line = cos(x1_line).*x2_line;

figure
hold on;
surfc(X1, X2, Z);
plot3(x1_line, x2_line, z_line, 'm', 'LineWidth',2);
grid on;
xlabel("x_1")
ylabel("x_2")
zlabel('z = f(x_1, x_2)')
title('A composite plot')
view([35 30])
% get rid of black lines in the surface plot
shading interp   
colormap(jet(50))
colorbar   % adds a colorbar that acts as a legend for colors
Surface + plot3
Fig. 13Surface + plot3[3]

References

[1] 2D Plotting in Matlab - Christopher's youtube tutorial

[2] 2D Plotting - matlab script download

[3] 3D Plotting in Matlab - Christopher's youtube tutorial

[4] 3D Plotting - matlab script download


HomeWikis
SnippetsAbout
Google ScholarLinkedIn