You are on page 1of 35

DIGITAL IMAGE

PROCESSING IN
MATLAB
LECTURE 3
Handling multiple plots
Displaying multiple plots in a graph- hold on
command

 X=1:1:50;
 Y=X.^3;
 A=1:1:100;
 B=A.^2;

 plot(X, Y, 'r')
 hold on;
 plot(A, B, 'g')
‘hold on’ example
4
x 10
14

12

10

0
0 10 20 30 40 50 60 70 80 90 100
The ‘figure’ command
 Opens up a new figure for the next plot
 X=1:1:50;
 Y=X.^3;
 A=1:1:100;
 B=A.^2;

 plot(X, Y, 'r')
 figure;
 plot(A, B, 'g')
‘figure’ example
4
x 10
14

12

10

8
10000

6 9000

8000
4
7000
2
6000

0 5000
0 5 10 15 20 25 30 35 40 45 50
4000

3000

2000

1000

0
0 10 20 30 40 50 60 70 80 90 100
To close existing figures

 >>close([1 3])
 closes figures 1 and 3

 >>close all
 closes all open figures (useful in scripts/functions)

 You can also copy paste or save a Matlab figure..


A really nice way of plotting multiple plots will be
like this……..but how to plot this?
How to display multiple plots in different windows of
a single figure?

 subplot(N, M, 1) or subplot(NM1)

 makes a figure with N rows and M columns of axes,


and activates the first axis or the first slot for
plotting

 each axis can have its own label, a legend, and a


title
An example…
 x1=1:1:50;
 y1=x1.^1;

 subplot(2, 2, 1)
 plot(x1, y1)
Consider these 4 sets
 x1=1:1:50;
 y1=x1.^1;

 x2=1:1:100;
 y=x2.^2;

 x3=1:1:75;
 y3=x3.^3;

 x4=1:1:60;
 y4=x4.^4
No of rows=2 and no of columns=2 in the main
figure…..

 subplot(2, 2, 1)
 plot(x1, y1, 'r')

 subplot(2, 2, 2)
 plot(x2, y2, 'g')

 subplot(2, 2, 3)
 plot(x3, y3, 'k')

 subplot(2, 2, 4)
 plot(x4, y4, 'b')
Playing with the Plot
Try this
Other properties
 title('Stress-Strain');
 xlabel(‘this is x axis');
 legend('Steel','Aluminum','Tungsten');
 ylabel(‘this is y axis’)
 grid on
Plot……
Stress-Strain
9
Steel
8 Aluminum
Tungsten

6
this is y ax is

1
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
this is x axis
Check out what these commands do to your plot

 xlim([-pi pi]);

 ylim([-1 1]);
To specify both at once, use axis:

axis ( [ XMIN XMAX YMIN YMAX ] )

axis([-pi pi -1 1]);
 sets the x axis limits between -pi and pi and the y axis

limits between -1 and 1

You can set the x and y limits to any values you wish.
Axis Modes
As your homework test these commands after a
plot to see what happens
An exercise
 Use MATLAB to plot the following function for
 -1≤ t ≤ 5 sec (in any increment you want).

 Force the x axis (time axis) limit to be from -1 to 5


 Label x and y axis and give the legend ‘my signal’
Solution graph
To plot in 2D your x and y should be
1D arrays like this (imagine)
BUT to plot in 3D, your x an y should be 2D arrays
like this:
Meshgrid
 It is a built in function of Matlab that extends a 1
dimensional array in 2 D BEFORE
MESHGRID
 Consider this:
 >>q=[1 2 3];
 >> Q=meshgrid(q) AFTER
 Q= MESHGRID

 1 2 3
 1 2 3
 1 2 3
Similarly..

 a=[1 2 3];
 b=[5 6 7];
 [X Y]= meshgrid(a, b)
Surface Plots
 In certain cases, it is better to visualize surfaces in
3D

 surf puts vertices at specified points in space x,


y, z and connects all the vertices to make a
surface

 The vertices can be denoted by matrices X,Y,Z


An exercise before moving forward..

 Define x and y
 x is a row vector starting from –pi and ending at pi
(increments= 0.1)
 y=x
 Apply meshgrid on x and y to get new ‘X’ and ‘Y’
 Define Z to be the multiplication product of sin(X)
and cos (Y).
Introducing the ‘surf’ command

 Now apply this command:

 >>surf(X, Y, Z)
Surf- to make a surface
Surf Options
Contour command

 Surf gives you a 3D plot.

 What if you want the entire information of a surf


plot in a 2D plot?

 Contour command helps you do that.


 The previous ‘surf’ plot that you just made, on the
same figure window, also plot this:
 >>contour(X,Y,Z,'LineWidth',2)

 Now in a new figure window, plot the same


contour command:
 >>contour(X,Y,Z,'LineWidth',2)
Contour
See this plot and rotate it in 3D to understand the
difference between surf and contour

 clear
 clc
 x=-pi:0.1:pi;
 y=-pi:0.1:pi;
 [X,Y]=meshgrid(x,y);
 Z =sin(X).*cos(Y);
 surf(X,Y,Z)
 hold on
 contour(X,Y,Z,'LineWidth',2)
0.2

-0.2

-0.4

-0.6

-0.8

-1

You might also like