Home » All » Specialized MATLAB 2 D Plots

Specialized MATLAB 2 D Plots

Specialized MATLAB 2 D Plots

In this section, we discuss all Specialized MATLAB 2 D Plots in MATLAB Software.

We all know the basic command for plotting in Matlab is 'plot'. Other specialized plots are given with examples in this section. Just copy the code and paste in command window or run with editor window. To see the plots in bigger size, click on the individual plot in second column. Here, the simple way to plot with two coordinates are given, just change the data of x and y to get your own plots. If you have any queries, send us message from Quick Contact on the right side.

Subscribe to Our YouTube Channel by Clicking ( HERE ) For  FREE Video Courses

1. Plot command

%% Ex 1: Plot   cos (x) , for 0\leqslant x\leqslant 2\pi
x=0:0.1:2*pi;
y=cos(x);
plot(x,y,'r--o')
xlabel('x')
ylabel('y')
title('x vs y')
p1
%% Ex 2: Plot   sin(5x)+cos(0.3x)+e^{-2x} , for 0\leqslant x\leqslant 2
x=0:0.1:2;
y=sin(5*x).*cos(0.3*x)+exp(-2*x);
plot(x,y,'r--o')
xlabel('x')
ylabel('y')
title('x vs y')
 p2
%% Ex 3: Plot   x. sin(x) , for 0\leqslant x\leqslant 10 \pi
x=0:0.1:10*pi;
y=x.*sin(x);
plot(x,y,'b--o')
xlabel('x')
ylabel('y')
title('x vs y')
 p3

2. fplot command

For plotting given function

 %% fplot ex  1: Plot   cos (x) , for 0\leqslant x\leqslant 2\pi using fplot
fplot('cos(x)',[0 2*pi],'r--o')
xlabel('x')
ylabel('y')
title('x vs y')
 f1
 %% fplot ex 2: Plot   sin(5x)+cos(0.3x)+e^{-2x} , for 0\leqslant x\leqslant 2  using fplot
fplot('sin(5*x)*cos(0.3*x)+exp(-2*x)',[0 2],'b--o')
xlabel('x')
ylabel('y')
title('x vs y')
 f2
 %% fplot ex 3 : Plot   x. sin(x) , for 0\leqslant x\leqslant 10 \pi using fplot
fplot('x.*sin(x)',[0 10*pi])
xlabel('x')
ylabel('y')
title('x vs y')
 f3

3. HIST Plot

%% hist plot ex 1: Plot random 100 values
y = randn(100,1);
hist(y)
xlabel('x')
ylabel('y')
title('x vs y')
 h1
%% hist plot ex 2:Plot 100x3 values
y = randn(100,3);
hist(y)
xlabel('x')
ylabel('y')
title('x vs y')
 h2

4. Polar Plot

%% polar plot ex 1: Draw the polar plot of 3cos^2(0.3 t)+2t^2 for 0\leqslant x\leqslant 2\pi
t=0:0.01:2*pi;
r=3*cos(0.3*t).^2+2*t.^2;
polar(t,r)
xlabel('x')
ylabel('y')
title('x vs y')
 pl1
%% polar plot ex 2:Draw the polar plot of r^2=10*sin(10t) for 0\leqslant x\leqslant 2\pi  
t=0:0.01:2*pi;
r = sqrt(abs(10*sin(10*t))) ;
polar (t,r )
xlabel('x')
ylabel('y')
title('x vs y')
 pl2

5. FILL Plot

 %% fill plot ex 1: Draw the polar plot of r^2=10*sin(10t) for 0\leqslant x\leqslant 2\pi
t=0:0.01:2*pi;
r = sqrt( abs ( 10* sin ( 10 *t ) ) ) ;
x = r.*cos( t ) ;
y= r.*sin(t ) ;
fill(x , y , ' m ' ) ,
axis('square' )
xlabel('x')
ylabel('y')
title('x vs y')
 fl1

6. Plotyy

%% plotyy: Plot two data sets on one graph using two y-axes

%% plotyy ex 1: Plot y_1=e^x, y_2=e^{-x}*cos(x) for 0\leqslant x\leqslant 10
x = 0:.4 : 10 ;
y1 = exp(x) ;
y2=exp (- x ).*cos( x ) ;
[HA,HL1,HL2] = plotyy(x ,y1 ,x ,y2 ) ;
ylabel(HA(1),'e^x')
ylabel(HA(2),'e^x sin (x)')
xlabel(HA(1),'Time (s)')
% xlabel(HA(2),'Hello')
set(HL1,'LineStyle','--','LineWidth',2);
set(HL2,'LineStyle',':','marker','o','LineWidth',2);
 yy1
%% plotyy ex 2 :Plot z_1=100*e^{-0.005*t}, z_2=sin(0.005*t) for 0\leqslant t\leqslant 900
t = 0:900;
z1 =100*exp(-0.005*t);
z2 = sin(0.005*t);
figure
[HA,HL1,HL2] = plotyy(t,z1,t,z2,'semilogy','plot');
ylabel(HA(1),'Semilog Plot') % label left y-axis
ylabel(HA(2),'Linear Plot') % label right y-axis
xlabel(HA(2),'Time') % label x-axis
set(HL1,'LineStyle','--','LineWidth',2);
set(HL2,'LineWidth',2);
grid(HA(1),'on');
 yy2
%%  plotyy ex 3:  Plot y=e^{sin(t)}, for 0\leqslant t\leqslant 2\ pi
t = 0:pi/20:2*pi;
y = exp(sin(t));
plotyy(t,y,t,y,'plot','stem')
 yy3

7. Logarithmic Plots in MATLAB

 %% loglog plot 1 : Draw the logarithmic plot of x=e^t ,  y= 500+ e^{2*t} for 0\leqslant t\leqslant 2\ pi
t=0:0.01:2*pi ;
x = exp (t ) ;
y=500 + exp (2*t ) ;
loglog(x , y),
grid
xlabel('x')
ylabel('y')
title('x vs y')
 ll1
%% semilogx plot 2: Draw the logarithmic plot of x=e^{-2*t },  y=t^{2} for 0\leqslant t\leqslant 2\ pi
t=0:0.01:2*pi ;
x = exp(-2*t);
y = t.^2 ;
semilogx (x , y) , grid
xlabel('x')
ylabel('y')
title('x vs y')
 lx1
%% semilogy plot 3: Draw the logarithmic plot of x=e^t ,  for 0\leqslant t\leqslant 2\ pi
t=0:0.01:2*pi ;
semilogy (t,exp(t) )
grid
xlabel('x')
ylabel('y')
title('x vs y')
 ly1

 8. Bar Plot

Bar plot ex 1: Draw the bar plot of x=e^{-t^2} ,  for -1\leqslant t\leqslant 1t = -1:0.2:1;
bar(t,exp(-t.^2),'g')
b1
Bar plot ex 2: Draw the bar plot of r^2=10*sin(t) and y=r*sin(t)  for -1\leqslant t\leqslant 2\pi
t=0:0.5:2*pi ;
r = sqrt(abs(10*sin(10*t))) ;
y =r.*sin(t) ;
% group
bar (t,y)
b2
Bar plot ex 3: Draw the bar plot
y = round(rand(5,3)*10);
subplot(2 , 2, 1)
bar(y, 'group')
title 'Group'
subplot(2, 2, 2)
bar(y, 'stack')
title 'Stack'
subplot(2, 2, 3)
barh(y, 'stack')
title 'Stack'
subplot(2, 2, 4)
bar(y,1.5)
title 'Width = 1.5'
b3
Bar plot ex 4: Draw the bar plot for students marks in a class
cont = char ( 's1 ' , 's2' , ' s3 ',...
' s4' , ' s5 ' ) ;
mar = [20 ; 80 ; 50 ; 75 ; 45] ;
barh (mar)
for i =1:5,
gtext (cont(i,:));
end
xlabel('Marks' )
title( 'Students Marks')
bh1

9. Error bar

Errorbar plot ex 1: Draw the error plot of sin(x) with its approximationx=0:0.1 : 2 ;
aprx2= x - x.^3/6 ;
er = aprx2 - sin (x ) ;
errorbar (x , aprx2 , er )
err1
Errorbar plot ex 2: Draw the error plot of cos(x) with its approximation
x=0:0.1 : 2 ;
aprx2= 1 - x.^2/2 ;
er = aprx2 - cos(x ) ;
errorbar (x , aprx2 , er )
err2

10. Area

Area plot ex 1x=-3*pi:0.1:3*pi;
%y =-sin (x)./x ;
y =-cos(x);
area( x , y)
xlabel( 'x ' ), ylabel (' sin(x)./x ' )
hold on
x1= x (96: 155 ) ; y1 = y( 96: 155) ;
area( x1,y1,'facecolor' ,'y ' )
ar1

11. pie

pie plot ex 1cont = char ( 's1 ' , 's2' , ' s3 ',...
' s4' , ' s5 ' ) ;
mar = [20 ; 80 ; 50 ; 75 ; 45] ;
pie(mar)
for i =1:5,
gtext (cont(i,:));
end
xlabel('Marks' )
title( 'Students Marks')
pi1
pie plot ex 2
x = [.19 .22 .41];
pie(x)
pi2

12. Stem

Stem plot ex 1t=0:0.1:2*pi ;
y = exp(-.2*t).*cos(t) ;
stem (t , y)
sm1

13. Stairs

Stairs Plot ex 1t=0:0.1:2*pi ;
% t=linspace(0,2*pi , 200) ;
r=sqrt(abs( 10*sin( 10 *t ) ) ) ;
y=r.*sin(t) ;
stairs (t,y)
axis([0 pi 0 inf ] ) ;
st1

14. Compass

%The compass function shows vectors emanating from the origin of a graph.
%The function takes Cartesian coordinates and plots them on a circular grid.

Compass plot ex 1x = randn(20,20);
y = eig(x);
compass(y)
cp1

15. Comet

Comet plot ex 1t=0:0.001:4*pi ;
y = t.*sin (t) ;
% y = sin (t) ; % Use this for 2nd exmple
comet(t,y)

16. Contour

contour plot ex 1n = -2.0:.2:2.0;
[X,Y,Z] =peaks(n);
[C, h] = contour(X,Y,Z,10);% Displays 2-D isolines generated from values given by a matrix Z.
colormap('default');
clabel(C,h)% Generates labels using the contour matrix and displays the labels in the current figure.
[U,V] = gradient(Z,.2);
hold on
quiver(X,Y,U,V)
hold off
ct1

17. Meshgrid

meshgrid ex 1r = -5:.2:5 ;
[X,Y] = meshgrid (r,r ) ;
Z =-0.5*X.^2+ X.*Y +Y.^2 ;
cs =contour (X , Y , Z) ;
clabel(cs )
mg1

18. Quiver

quiver ex 1[X , Y] = meshgrid(r,r) ;
Z = X.^2-5*sin(X.*Y)+ Y.^ 2 ;
[dx,dy]=gradient(Z,.2 ,.2 ) ;
quiver(X,Y,dx,dy,2) ;
qv1

19. Pcolor

%pcolor(C) draws a pseudo color plot

pcolor ex 1r = -2:.2:2 ;
[X,Y] =meshgrid(r,r) ;
Z=X.^2-5*sin(X.*Y)+ Y.^2 ;
pcolor(Z), axis('off' )
shading interp
pc1

20. Feather

% feather plot ex 1
th=-pi:pi/10:pi ;
x=cos (th) ;
y=sin (th) ;
feather(x+i*y) % z=complex(x,y) and then feather(z)
 Try Yourself

21. Rose

% rose plot ex 1t=0:pi/100:2*pi ;
rose(t)
 Try Yourself