Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

MATLAB PLOTTING

Plot:
Code: plot(X,Y, <specifications>)

Example:
x = 0:pi/100:2*pi; x = linspace(-2*pi,2*pi);
y = sin(x); y1 = sin(x);
plot(x,y) y2 = cos(x);

figure
plot(x,y1,’o-‘,x,y2,’g--‘)
Display markers at every fifth data point: x = linspace(0,10,150);
x = linspace(0,10); y = cos(5*x);
y = sin(x);
plot(x,y,'-','MarkerIndices',1:5:length(y)) figure
plot(x,y,'Color',[0,0.7,0.9])
Line style: “-“, “--”, “:”, “-.” Circle:
Markers: o/+/*/./x/s/d/^/v/>/< r = 2;
Colours: r/g/b/c/m/y/k/w xc = 4;
yc = 3;

theta = linspace(0,2*pi);
x = r*cos(theta) + xc;
y = r*sin(theta) + yc;
plot(x,y)
axis equal

* Other command option: area, semilogx, semilogy, loglog

Scatter:
Code: scatter(X,Y) ; scatter(X,Y,<size.>,<colour>)

Example:
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
sz = linspace(1,100,200);
c = linspace(1,10,length(x));
scatter(x,y,sz,c)

* Other command option: bubblechart

Histogram:
Command: histogram(X) ; histogram(X,<bin_number>) ; histogram(X,<bin_vector>)
Example:
X = randn(1000,1); x = randn(1000,1);
h = histogram(X) edges = [-10 -2:0.25:2 10];
x = randn(1000,1); h = histogram(x,edges);
nbins = 25;
h = histogram(x,nbins)
Bar plot:
Command: bar(Y) ; bar(X,Y) ; bar(X,Y,<properties>)
Example:
y = [75 91 105 123.5 131 150 179 203]; x = 1900:10:2000;
bar(y) OR bar(y, ‘r’) y = [75 91 105 123.5 131 150 179 203
226 249 281.5];
bar(x,y)
X= ["Spring" "Summer" "Autumn" "Winter"]; y = [75 91 105 123.5 131 150 179 203
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12]; 226 249 281.5];
bar(x,y) OR bar(x,y,’stacked’) bar(y,'FaceColor', [0 .5 .5],
'EdgeColor', [0 .9 .9],
'LineWidth',1.5)

* barh : horizontal bars.

Errorbar plot:
Command: errorbar(y,err) ; errorbar(x,y,err) ; errorbar(x,y,neg,pos)
Example:
x = 1:10:100; x = 1:10:100;
y = [20 30 45 40 60 65 80 75 95 90]; y = [20 30 45 40 60 65 80 75 95 90];
err = [5 8 2 9 3 3 8 3 9 3]; neg = [5 8 2 9 3 3 8 3 9 3];
errorbar(x,y,err) pos = neg+2;
errorbar(x,y,neg,pos)

Fplot:
Command: fplot(f) ; fplot(f,xinterval); fplot(__, <properties>)

Example:
fplot(@(x) sin(x)) xt = @(t) cos(3*t);
yt = @(t) sin(2*t);
for x in default interval [-5 5]. fplot(xt,yt)

fplot(@(x) exp(x),[-3 0],'b') fplot(@(x)


hold on sin(x+pi/5),'Linewidth',2);
fplot(@(x) cos(x),[0 3],'b') hold on
hold off fplot(@(x) sin(x-pi/5),'--or');
grid on fplot(@(x) sin(x),'-.*c')
hold off
* Similar command: fsurf (3D)

Pie chart:
Command: piechart(data) ; piechart(data,names)
Example:
data = [1 2 3 4]; xt = @(t) cos(3*t);
piechart(data) yt = @(t) sin(2*t);
fplot(xt,yt)
% Change the labels
p.Labels = p.Names + " (" + string(data) + ")";
3D line plot:
Command: plot(X,Y, Z,<specifications>)
Example:
t = 0:pi/50:10*pi; t = 0:pi/500:pi;
st = sin(t); xt1 = sin(t).*cos(10*t);
ct = cos(t); yt1 = sin(t).*sin(10*t);
plot3(st,ct,t) zt1 = cos(t);

OR xt2 = sin(t).*cos(12*t);
plot3(xt,yt,t,'-o', yt2 = sin(t).*sin(12*t);
'Color','b','MarkerSize',10) zt2 = cos(t);

plot3(xt1,yt1,zt1,xt2,yt2,zt2)
* Similar command: scatter3

3D histogram:
Command: histogram2(X,Y); histogram2(X,Y,<bins>); histogram2(X,Y,<bin_vec>)
Example:
x = randn(10000,1); x = randn(1000,1);
y = randn(10000,1); y = randn(1000,1);
h = histogram2(x,y) nbins = 5;
h = histogram2(x,y,nbins)
* Similar command: bar3

Surf:
Command: surf(X,Y,Z)
Example:
[X,Y] = meshgrid(1:0.5:10,1:20); [theta,z] = meshgrid(0:0.1:2*pi,-1:0.1:1);
Z = sin(X) + cos(Y); X = sqrt(1-z.^2).*cos(theta);
surf(X,Y,Z) Y = sqrt(1-z.^2).*sin(theta);
Surf(X,Y,z)
* Similar command: contour, surfl, mesh, surfc

You might also like