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

Computer Programming

(CSC209)

Lecture (7)
2-D Plot and Subplot Functions

Dr.Omar Amutairi
Bar Chart
x = [1:10];
y = 21:30;
bar(x,y),xlabel('Student'),ylabel('Score'),
title('First Sem:')
print -deps graph.eps %table 2.9
First Sem:
30

25

20
Score

15

10

0
1 2 3 4 5 6 7 8 9 10
Student
2
Barh Chart
x = [1:10];
y = 21:30;
barh(x,y),
xlabel('Student'),ylabel('Score'),
title('First Sem:')
First Sem:

10

7
Score

0 5 10 15 20 25 30 3
Student
Pie Chart
x = [1:10];
y = 21:30;
pie(x,y),
xlabel('Student'),ylabel('Score'),
title('First Sem:')
4% First
2%Sem:
5% 18%

7%

9%

16%

11%

15%
13%
4
Stair Chart
x = [1:10];
y = 21:30;
stairs(x,y),
xlabel('Student'),ylabel('Score'),
title('First Sem:')
First Sem:
30

29

28

27

26
Score

25

24

23

22

21
1 2 3 4 5 6 7 8 9 10 5
Student
Stem Chart
x = [1:10];
y = 21:30;
stem(x,y),
xlabel('Student'),ylabel('Score'),
title('First Sem:')
First Sem:
30

25

20
Score

15

10

0
1 2 3 4 5 6 7 8 9 10 6
Student
Drawing Contours
% g = f(x,y), where -5 ≤ x ≤ 5, -3 ≤ y ≤ 3
[x,y] = meshgrid(-5:0.1:5, -3:0.1:3);
%independent variables
g = x.^2 + y.^2; % our function
contour(x,y,g) % call the contour function

-1

-2

-3 7
-5 -4 -3 -2 -1 0 1 2 3 4 5
Polar Plots
g = 0.5;
theta = linspace(0,2*pi,41);
gain = 2*g*(1+cos(theta));
% Plot gain
polar(theta,gain,'r-');
title ('\bfGain versus angle \it{\theta}');
Gain versus angle 
90 2
120 60
1.5

150 1 30

0.5

180 0

210 330

240 300
270
8
Polar Plots
g = 0.5;
theta = linspace(0,2*pi,41);
gain=(exp(-0.1.*theta)).*sin(5*theta)
% gain = 2*g*(1+cos(theta));
% Plot gain
polar(theta,gain,'r-');
Gain versus angle 
90
1
120 60
0.8

0.6
150 30
0.4

0.2

180 0

210 330

240 300
270
9
Multiple Scale (plotyy)
clear all; close all; clc
x=0:0.01:20;
y1=200*exp(-0.05*x).*sin(x);
y2=0.8*exp(-0.5*x).*sin(10*x);
[AX, H1, H2] = plotyy(x,y1,x,y2,'plot');
set(get(AX(1), 'ylabel'),'string', 'Left Y-axis')
set(get(AX(2), 'ylabel'),'string', 'Right x-axis')
xlabel('0 to 20 sec')
set(H1, 'linewidth',2, 'linestyle','--')
set(H2, 'linewidth',2, 'linestyle',':')
200 0.8

150 0.6

100 0.4

50 0.2

Right x-axis
Left Y-axis

0 0

-50 -0.2

-100 -0.4

-150 -0.6

-200 -0.8
10
0 2 4 6 8 10 12 14 16 18 20
0 to 20 sec
log Scale (loglog plot)

50
10

40
10

30
10

20
10

10
10

0
10
-1
10
0
10
1
10
2
10
11
semilogy Scale (semilogy plot)
x=0:1:10;
y=10.^x;
semilogy(x,y) % y scale consider logarithmic value
grid on
10
10

8
10

6
10

4
10

2
10

0
10
0 1 2 3 4 5 6 7 8 9 10
12
semilogx Scale (semilogx plot)
x=0:1:10;
y=10.^x;
semilogx(x,y) % x scale consider logarithmic value
grid on 9
x 10
10

0
0 1
10 10 13
Graphics - Subplot

• subplot(m,n,1) % Makes an mxn array


for plots. Will place plot in 1st position.

Here m = 2 and n = 3.
Subplot Mapping
SUBPLOT- display multiple axes in the same figure
window
subplot(#rows, #cols, index)
»subplot(2,2,1);
»plot(1:10)

»subplot(2,2,2)
»x = 0:.1:2*pi;
»plot(x,sin(x))

»subplot(2,2,3)
»x = 0:.1:2*pi;
»plot(x,exp(-x),’r’)

»subplot(2,2,4)
»plot(peaks)
Subplot (1 row in 2 colmn)
x = [0:0.01:5];
y = exp(-1.5*x).*sin(10*x);
subplot(1,2,1)
plot(x,y), xlabel('x'),
ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -1 1])
y1 = exp(-2*x).*sin(10*x);
subplot(1,2,2)
plot(x,y1),xlabel('x'),ylabel('exp(–2x)*sin(10x)')
axis([0 5 -1 1]) 1 1

0.8 0.8

0.6 0.6

0.4 0.4
exp(–1.5x)*sin(10x)

exp(–2x)*sin(10x)
0.2 0.2

0 0

-0.2 -0.2

-0.4 -0.4

-0.6 -0.6

-0.8 -0.8

-1 -1
0 1 2 3 4 5 0 1 2 3 4 5
x x
Subplot (2 row in 2 colmn)
x = [0:0.01:5];
y = exp(-1.5*x).*sin(10*x);
subplot(2,1,1)
plot(x,y), xlabel('x')
y = exp(-2*x).*sin(10*x);
subplot(2,1,2) 1

plot(x,y)
0.5

-0.5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x

0.5

-0.5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
17
Subplot (2 row in 2 colmn)
x=-6:0.01:6;
y=sin(x); y1=cos(x); y2=cos(x)+sin(x);
subplot(2,1,1)
plot(x,y) 1
subplot(2,2,3)
0.5
plot(x,y1)
0
subplot(2,2,4)
plot(x,y2) -0.5

-1
-6 -4 -2 0 2 4 6

1 2

0.5 1

0 0

-0.5 -1

-1 -2
-10 -5 0 5 10 -10 -5 0 5 10
Subplot (2 row in 2 colmn)
x = 0:0.2:100;
y = 2 * x.^2;
subplot(2,2,1)
plot(x,y);
title('Linear/linear Plot');
subplot(2,2,2) 4 4
x 10 Linear / linear Plot x 10 Log / linear Plot

semilogx(x,y); 1.5
2

1.5
2

title('Log/linear Plot'); 1 1

y
subplot(2,2,3) 0.5 0.5

0 0
semilogy(x,y); 0 50
x
100 10
-2
10
x
0 2
10

title('Linear/log Plot'); 5
Linear / log Plot 5
Log / log Plot
10 10

subplot(2,2,4) 0
10
0
10
y

y
loglog(x,y); -5 -5

title('Log/log Plot'); 10
0 50
x
100
10
10
-2
10
x
0 2
10

19
3D Plotting (plot3)
x=linspace(0,2*pi,50);
y=sin(x);
z=x'*y;
plot3(x,y,z)

10

-5

-10
1
0.5 8
0 6
4
-0.5 2
-1 0

20
3D Plotting (mesh)
x=linspace(0,2*pi,50);
y=sin(x);
z=x'*y;
mesh(x,y,z)

10

-5

-10
1
0.5 8
0 6
4
-0.5 2
-1 0 21
3D Plotting (Surf)
x=linspace(0,2*pi,50);
y=sin(x);
z=x'*y;
surf(x,y,z)

10

-5

-10
1
0.5 8
0 6
4
-0.5 2
-1 0
22
3D Plotting (contour)
close all, clear all, clc
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
contour(X,Y,Z,'ShowText','on')

23
3D Plotting (contour(peaks))
contour(Peaks)

45

40

35

30

25

20

15

10

5 10 15 20 25 30 35 40 45 24
END

25

You might also like