- 2D Line Plot
Displays the relationship between two variables
clear; % Clear all variables from the workspace
clc; % Clear the command window
close all; % Close all figure windows
x = linspace(1,200,100); % Generate 100 evenly spaced values between 1 and 200
y1 = log(x) + 1; % Create function y = log(x) + 1
y2 = log(x) + 2; % Create function y = log(x) + 2
figure; % Create a new figure window
plot(x,y1); % Plot y = log(x) + 1
hold on % Enable plotting multiple lines in the same window
plot(x,y2,'LineWidth',2); % Plot y = log(x) + 2 with line width 2
hold off % Disable multiple plots
legend('y1','y2'); % Add legend for y1 and y2
- 2D Scatter Plot
figure;
y3 = y1 + rand(1,100) - 0.5;
plot(x,y1,'LineWidth',2,'Color',[0.21,0.21,0.67]);
hold on
% Set marker shape, fill color, and edge color
plot(x,y3,'o','LineWidth',2,'Color',[0.46,0.63,0.90],'MarkerFaceColor',[0.35,0.90,0.89],'MarkerEdgeColor',[0.18,0.62,0.17]);
hold off
- 2D Colormap Plot
Use different colors and point sizes to represent varying values
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200); % Randomly generate 1x200 values between [0,1]
sz = 25; % Size of points
c = linspace(1,10,length(x));
scatter(x,y,sz,c,'filled')
- 2D Bar Chart
A = [60.689;87.714;143.1;267.9515];
C = [127.5;160.4;231.9;400.2];
B = C - A;
D = [A,B,C];
bar1 = bar([2:5:17],A,'BarWidth',0.2,'FaceColor','k'); % Plot A data
hold on;
bar2 = bar([3:5:18],B,"BarWidth",0.2,'FaceColor',[0.5 0.5 0.5]);% Plot B data
hold on;
bar3 = bar([4:5:19],C,'BarWidth',0.2,'FaceColor','w'); % Plot C data
ylabel('Time/s'); % Label Y-axis
xlabel('GMM Order'); % Label X-axis
legend('Training Time','Testing Time','Total Time'); % Add legend
labelID = {'8th Order','16th Order','32nd Order','64th Order'}; % Define tick labels
set(gca,'XTick',3:5:20); % Set X-axis ticks
set(gca,'XTickLabel',labelID); % Set X-axis tick labels
- 2D Filled Plot
x = 0.4:0.1:2*pi; % Define x
y1 = sin(2*x); % Define y1
y2 = sin(x); % Define y2
% Determine upper and lower boundaries
maxY = max([y1;y2]);
minY = min([y1;y2]);
% Define filled polygon in clockwise order
xFill = [x,fliplr(x)];
yFill = [maxY,fliplr(minY)];
figure
fill(xFill,yFill,[0.21,0.21,0.67]); % Fill area
hold on
% Draw contour lines
plot(x,y1,'k','LineWidth',2) % Plot y1 vs x
plot(x,y2,'k','LineWidth',2) % Plot y2 vs x
hold off
- Multi-Y Axis Plot
figure;
load('accidents.mat','hwydata' ) % Load data from "accidents.mat" file
ind = 1:51;
drivers = hwydata(:,5); % Extract 5th column of hwydata
yyaxis left % Create Y-axis on the left
scatter(ind,drivers,'LineWidth',2); % Plot scatter plot
title('Highway Data'); % Set title
xlabel('States'); % Set X label
ylabel('Licensed Drivers(thousands)'); % Set Y label
pop = hwydata(:,7); % Extract 7th column of hwydata
yyaxis right % Create Y-axis on the right
scatter(ind,pop,'LineWidth',2); % Plot scatter plot
ylabel('Vehicle Miles Traveled(millions)'); %
- 2D Vector Field Plot
[x,y] = meshgrid(0:0.1:1,0:0.1:1);
u = x;
v = -y;
startx = 0.1:0.1:0.9;
starty = ones(size(startx));
% Get properties of all streamlines
figure;
quiver(x,y,u,v); % Plot vector field
streamline(x,y,u,v,startx,starty); % Plot streamline
- 3D Curve Plot
figure;
t = 0:pi/20:10*pi;
xt = sin(t);
yt = cos(t);
plot3(xt,yt,t,'-o','Color','b','MarkerSize',10); % Plot 3D curve
- 3D Scatter Plot
figure;
[X,Y,Z] = sphere(16);
x = [0.5*X(:);0.75*X(:);X(:)];
y = [0.5*Y(:);0.75*Y(:);Y(:)];
z = [0.5*Z(:);0.75*Z(:);Z(:)];
S = repmat([70,50,20],numel(X),1);
C = repmat([1,2,3],numel(X),1);
s = S(:);
c = C(:);
h = scatter3(x,y,z,s,c);
h.MarkerFaceColor = [0 0.5 0.5];
x = linspace(1,20,100);
y1 = log(x) + 1;
y2 = log(x) + 2;
y3 = y1 +rand(1,100) - 0.5;
figure;
scatter3(x,y2,y3,x,x,'filled');
- 3D Pseudocolor Plot
[x,y,z] = peaks(30);
figure;
plot1 = subplot(1,2,1);
surf(x,y,z);
% Get colormap for first plot, default is parula
plot2 = subplot(1,2,2);
surf(x,y,z);
% Set color for second plot
colormap(hot);
% Set color for first plot as parula
% One axis
figure;
h1 = surf(x,y,z);
hold on;
h2 = surf(x,y,z+5);
hold off;
colormap(hot);
- Clipped Pseudocolor Plot
figure;
n = 300;
[x,y,z] = peaks(n);
subplot(2,2,[1,3]);
surf(x,y,z);
shading interp;
view(0,90);
for i = 1:n
for j = i:n
if x(i,j)^2 + 2 * y(i,j)^2 > 6 && 2 * x(i,j)^2 + y(i,j)^2 < 6
z(i,j) = NaN;
end
end
end
subplot(2,2,2)
surf(x,y,z);
shading interp
view(0,90)
subplot(2,2,4)
surf(x,y,z);
shading interp
- Contour Plot
figure;
[X,Y,Z] = peaks;
subplot(2,2,1);
contour(X,Y,Z,20,'LineWidth',2);
subplot(2,2,2);
contour(X,Y,Z,20,'--',"LineWidth",2)
subplot(2,2,3);
v = [1,1];
contour(X,Y,Z,v,'LineWidth',2);
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
subplot(2,2,4);
contour(X,Y,Z,'ShowText','on','LineWidth',2);
- 3D Contour Plot
figure('Position',[0,0,900,400]);
subplot(1,3,1);
[X,Y,Z] = sphere(50);
contour3(X,Y,Z,'LineWidth',2);
[X,Y] = meshgrid(-2:0.25:2);
Z = X.*exp(-X.^2 - Y.^2);
subplot(1,3,2);
contour3(X,Y,Z,[-0.2 -0.1 0.1 0.2],'ShowText','on','LineWidth',2)
[X,Y,Z] = peaks;
subplot(1,3,3);
contour3(X,Y,Z,[2 2],'LineWidth',2);
- Filled Contour Plot
figure;
subplot(2,2,1);
[X,Y,Z] = peaks(50);
contourf(X,Y,Z);
subplot(2,2,2);
contourf(X,Y,Z,'--');
% Limit range
subplot(2,2,3);
contourf(X,Y,Z,[2 3],'ShowText','on');
subplot(2,2,4);
contourf(X,Y,Z,[2 2]);
- 3D Vecter Field Plot
figure;
[X,Y,Z] = peaks(30);
% Vector field, surface normals
[U,V,W] = surfnorm(X,Y,Z);
% Arrow length and color
quiver3(X,Y,Z,U,V,W,0.5,'r');
hold on
surf(X,Y,Z);
xlim([-3,3])
ylim([-3,3.2]);
shading interp
hold off
view(0,90);
- Pseudocolor Plot with Projections
clear;clc;close all;
x = linspace(-3,3,30);
y = linspace(-4,4,40);
[X,Y] = meshgrid(x,y);
Z = peaks(X,Y);
Z(5:10,15:20) = 0;
z1 = max(Z);
z2 = max(Z,[],2);
figure;
subplot(3,3,[1,2]);
plot(x,z1,'LineWidth',2);
subplot(3,3,[6,9]);
plot(z2,y,'LineWidth',2);
subplot(3,3,[4,5,7,8]);
surf(x,y,Z);
xlim([-3,3]);
ylim([-4,4]);
view(0,90);
shading interp% Smooth image
- Heatmap
clear;clc;
z = rand(50);
z(z >= 0.0 & z < 0.6) = 0.5;
z(z >= 0.6 & z < 0.8) = 0.7;
z(z >= 0.8 & z <= 1) = 0.9;
for i = 1:30
z(randi(50,1,1):end,i) = nan;
end
for i = 31:50
z(30 + randi(20,1,1):end,i) = nan;
end
z(20:25,40:45) = nan;
figure;
% ax = surf(z);
ax = pcolor(z);
view(0,90);
ax.EdgeColor = [1 1 1];
- Molecular Model Plot
clear;clc;
% Coordinates for a sphere to make it look smooth, set to 100
[x,y,z] = sphere(100);
% Size of C
C = 10;
% Size of H
H = 5;
figure;
% Large sphere
surf(C*x,C*y,C*z,'FaceColor',"red","EdgeColor","none")
hold on
% Four small spheres, slightly offset
surf(H*x,H*y,H*z+10,'FaceColor','blue','EdgeColor','none');
surf(H*x + 10,H*y,H*z - 3,'FaceColor','blue','EdgeColor','none');
surf(H*x - 4,H*y -10,H*z -3,'FaceColor','blue','EdgeColor','none');
surf(H*x - 4,H*y +10,H*z - 3,'FaceColor','blue','EdgeColor','none');
% Axis settings
axis equal off
% Light source for depth perception
light
% Lighting none, disable light source
- Fractal Plot