1
1
MATLAB includes good tooh:; for visualization. Basic 2-D plots, fancy 3-D graphics
with lighting and colorrnaps, complete user control of the graphics objects through
Handle Gmphics tools for designing sophisticated graphics user interfaces, and an
imation are now part of MATLAB . What is special about MATLAB 's graphics
facility is its ease of use and expandability. Commands for most garden-variety
plotting are simple, easy to use, and intuitive. If you are not satisfied with what
you get , you can control and manipulate virtually everything in the graphics win
dow. This, however, requires an understanding of Handle Graphics, a system of
low-level functions to manipulate graphics objects. In this section, we take you
through the main features of MATLAB 's graphics facilities.
The style- option in the plot command is a character string that consists of one, two,
or three characters that specifY the color an d/or line style. There are several color,
line, and marker style-options:
The style-option is made up of the color option, the line option, the marker
option, or a combination of them.
Examples:
plot (x , y , 'r') plots y versus x with a red solid line,
plot (x , y , ' : ') plots y versus x with a dotted line,
plot (x , y , ' b-- ' ) plots y versus x with a blue dashed line, and
plot (x , y , '+') plots y versus x as unconnected points marked by +.
6.1.2 Lab els , t itle, legend , and other t ext obj ects
Legend
Legends on plots can be produced using the I n se rt ___, l ege n d button in the figure
window toolbar (see Fig. 6 . 1 ) or with the l egend command. The legend command
produces a boxed legend on a plot , as shown, for example, in Fig. 6.3 on page 182 .
The l e g end command is quite vertlatile. lt can take several o p t io nal argument s .
The mo st commonly used formH of t h e command are listed here .
le g end ( s t r i ngl , s t ring2, . . ) produces legend using the LcxL in
str'"ing l , string2, etc. , as labeb,
legend ( L i neSty L el , s t r i ngl , . . ) specifies the line style of each label,
legend ( . . , p o s ) writes the legend outside the plot-frame
if pos = - 1 and inside if pos = 0 ,
(there are other options for pos too) , and
l e ge nd off deletes the legend from the plot .
When MATLAB is asked to produce a legend, it tries to find a place on the plot
where it can write the specified legend without running into lines, grids, and other
graphics objects. The optional argument pos specifies the location of the legend
box. pos=1 places the legend in the upper right-hand corner (default) , 2 in the
upper left-hand corner, 3 in the lower left-hand corner, and 4 in the lower right
hand corner. The user, however, can move the legend at will with the mouse (click
and drag) . For more information, see Lhe on-line help on l egend.
Once a ploL is generated, you can change the axes limits with the axi s command.
Typing
axi s ( [xmin xmax ymin ymax] )
changes the c urr ent axes limits to the specified new values .Tmin and xmax for the
x-axis and ymin and ymax for the y-axis.
Examples:
axi s ( [ - 5 1 0 2 22] ) ; sets the x-axis from -5 to 10, y - axis from 2 to 22,
axy = [ - 5 1 0 2 22] ; axi s ( ax y ) ; same as above, and
ax = [ -5 1 0 ] ; ay= [2 22] ; axi s ( [ax ay ] ) ; same as above.
The axi s command may thus be used to zoom in on a particular section of the
plot or to zoom out . 1 There are also some useful predefined string arguments for
t he axi s command:
axi s ( ' equal ' ) sets equal scale on both axes,
ax i s ( ' square ' ) �;ets the default rectangular frame to a square,
ax i s ( ' normal ' ) reflets th e axis to defuult values ,
ax i s ( ' axi s ' ) freezes the current axP.s limiLR, and
axi s ( ' of f ' ) removes the surrounding frame and t he tick marks.
The axi s command must come after the plot command to have the desired effect .
alternatives t o the plot command we have j ust di s cussed . There is a w hole suite
of e z plotter functions, such as ezplot , e z p o l ar , ezcont our , that are truly easy
to use. See Section 3 . 8 for a discussion and examples of t he se functions.
Here, we provi de a li st of other funct i ons commonly used for plotting x-y data:
s emi l ogy makes semilog plot with log scale on the y-axis,
stairs plots a stair graph, and
st em plots a stem graph.
s em i l ogx
t = l inspace ( 0 , 2*pi , 200) ;
x exp ( - t ) ; y = t ;
=
semilogx ( x , y) , gri d
gr id
' 2
x = e , y = 100 + e \ 0 ::; t ::; 27r
loglog t l inspace ( 0 , 2*pi , 2 0 0 ) ;
x = exp (t ) ;
y 1 0 0 + exp ( 2 *t ) ;
loglog ( x , y) , gr id
6 . 1 Basic 2-D Plots 185
2
r = 2 sin 5t, 0 ::::; t ::::; 271"
polar t = l inspace ( 0 , 2*pi , 2 0 0 ) ;
r = sqrt ( abs ( 2 * s in ( 5 *t ) ) ) ;
polar ( t , r )
270
2
r 2 sin 5t, 0 ::::; t ::::; 271"
x r cos t , y = r sin t
t l i n s p ace ( 0 , 2 * p i , 2 0 0 ) ;
f ill
r = sqrt ( abs ( 2 * s in ( 5 *t ) ) ) ;
x = r . *cos (t) ;
y r . * s in ( t ) ;
fill (x , y , ' k ' ) ,
axi s ( ' s quare ' )
2
r 2 sin 5t, 0 ::::; t ::::; 271"
y r sin t
x3
fapprox x - 3! , o ::; x ::; 2
error fapprox - sin x
error bar
X - Q ; .1:2;
aprx2 = x - x . '3/6 ;
er aprx2 - s in (x ) ;
�
errorbar (x , aprx2 , er )
6.2 Using subplot for Multiple Graphs 1 89
subplot (m , n , p)
Subplot divides the graphics window into m x n subwindows and puts the plot
generated by the next plotting command into the pth subwindow, where the sub
wind ows are counted row-wise. Thus, the command subp lot (2 , 2 , 3) , plot (x , y)
divides the gr aphics window into four subwindows and plots y versus x in the third
subwindow, which is the first subwindow in the second row. For an example, see
Fig. 6.5 on page 1 9 1 .
\
P lot of a parametric space curve:
l
= t2 , z(t) =
:
x (t) t, y (t) t3 .
o
=
O :S t :S l .
"'u
0.6
.,. o.4 ..J!
! \
�0-·� JJI� "-"
t = l inspace ( 0 , 1 , 1 0 0 ) ;
I
p
plot3 I
X = t ; y = t . A 2 ; z = t . A3 ; o, J
plot3 ( x , y , z ) , gr id
0' .
xlabel ( ' x ( t) t')
ylabel ( ' y ( t ) t2 ' ) o.• 0.6 �·
z labe l ( ' z ( t ) t3 ' ) 0.2 � 0.4
0 0 0.2
y(t) - � x(l) = l
X = [0 0 0 0 ; 1 1 -1 1 ;
1 - 1 - 1 - 1] ;
y = [0 0 0 0 ; 4 4 4 4 ;
f i l13 4 4 4 4] ;
z = [0 0 0 0 ; 1 1 - 1 - 1 ;
- 1 1 1 -1] ;
f i llcolor=rand ( 3 , 4) ;
f i l l 3 ( X , Y , Z , f i llcolor)
view ( 1 20 , 30 )
z - - -,:----;
5
- :;-----;c
1 + x2 + y2 '
l x l :S 3, I Y I :S 3 .
contour3
·3.5
r = l inspace ( -3 , 3 , 50 ) ;
[x , y] = meshgrid (r , r ) ;
z = -5 . / ( 1 + x . A 2 + y . A 2 ) ;
c ontour3 ( x , y , z )
� -3
6 . 3 3-D Plots 197
- YR
4
z cos x cos y e
/x/ :S: 5, /y/ :S: 5
u = -5 : . 2 : 5 ;
surf
[X , Y] = me shgr i d ( u , u) ;
Z = c o s ( X ) . * c o s (Y) . * · · ·
exp ( - s qrt ( X . - 2 + Y . - 2 ) /4 ) ;
surf ( X , Y , Z )
-�
z cos x cos y e 4
u = -5 : . 2 : 5 ;
[X , Y] = me shgr i d ( u , u ) ;
surf c
Z = c o s ( X ) . * c o s (Y ) . * . . .
exp ( - sqrt ( X . - 2 + Y . - 2 ) /4) ;
surf c ( Z )
view ( -37 . 5 , 20 )
axi s ( ' of f ' )
-�
z cos x cos y e 4
u = -5 : . 2 : 5 ;
[X , Y] = meshgrid ( u , u) ;
surfl
Z = c o s ( X ) . * c o s (Y) . * . . .
e xp ( - sqrt ( X . - 2 + Y . - 2 ) /4) ;
surf l ( Z )
shading int erp
c o l o rmap hot
Not e : Plotting a s mface with surf ( X , Y , Z ) shows proper values on the x-axis and
the y-axis, whereas plotting the surface with surf (Z) shows the row and colnmn
i n dice s of m atrix Z on the
x-axis and the y-axis. The same is true for other 3-D
plotting commands such as mesh and cont our3. Compare the val ues on the x-axis
and the v-axis in the first and the last figure in this t able .