0% found this document useful (0 votes)
3 views

1

Uploaded by

avi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

1

Uploaded by

avi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

• Graphics

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.

6.1 Basic 2-D Plots for oncline helP;


type:
The most basic and perhaps most useful command for producing a 2-D plot is '1ielp grCipli:2a ··· ·

plot ( xvalues, yvalues, 'style-option )


where xvalues and yvalues are vectors containing the x- and y-coordinates of points
on the graph and the style-option is an optional argument that specifies the color,
the line style (e.g. , solid, dashed, dotted) , and the point-marker style (e.g. , o, + ,
*) . All three style options can be specified together. The two ve ct or s xvalues and
yvalues MUST have the same length. Unequal length of the two vectors is the most
common s ource of error in the plot commaml. The plot function also works with a
single-vector argument , in which case the elements of the vector are plotted against
row or column indices. Thus, for two column vectors x and y each of length n ,
plot (x , y) plntR v vmRnR x with a solid line (the default line stylP.) ,
plot ( x , y , ' - - ' ) p l o t s y versus x with a dashed line ( more on this below ) , and
plot (x) plots the elements o f x agai ns t their row index.
176 Graphics

6.1.1 Style options

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:

Color Style-option Line Style-option Marker Style-option

y yellow - solid + plus sign


m magenta -- dashed 0 circle
*
c cyan : dotted asterisk
r red - dash-dot X x-mark
g green none no line point
'
b blue up triangle
w white s square
k black d diamond, etc.

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 +.

When n o style-option i s specified, MATLAB uses a blue solid line by default .

6.1.2 Lab els , t itle, legend , and other t ext obj ects

Plots may be annotated with xlabel , ylabe1 , t it l e , and t ext commands.


The first three commands take string arguments, whereas the last one requires
three arguments-text (x-coordinate, y- coordinate, 'text ') , where the coordinate val­
ues are taken from the current plot. Thus,
xlabel ( ' Pipe Length ' ) labels the x-axis with Pipe Length,
ylabel ( ' Fluid Pres sure ' ) labels the y-axis with Fluid Pressur e ,
t i t l e ( ' Pres sure Var i at i on ' ) titles the plot with Pressure Variat i on, and
text ( 2 , 6 , ' Note this dip ' ) writes " N o t e thi s dip" at the location
( 2.0,6.0 ) in the plot coordinates.
We have already seen an example of xlabel , ylabel, and t i t l e in Fig. 3.9. An
example of t ext appears in Fig. 6 . 2 . The arguments of t ext ( x, y, 'text ') command
may be vectors, in which case x and y must have the same length and text may be
Fo·r on-line help just one string or a vector of strings. If text is a vector, then it must have the same
type: length as x. A useful variant of the t ext command is gt ext , which only takes a
help graph2d string argument (a t;ingle string or a vector of strings ) and lets the user specify the
location of the text by clicking the mouse at the desired location in the graphics
window.
6.1 Basic 2-D Plots 1 77

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.

6.1.3 Axis control , zoom in, and zoom out

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 .

1 There is also a z o om command that r.an he used lu w


: om in and zoom out using; thu mou�e in
the figure window. See the on-line help on zoom.
6 . 1 Basic 2-D Plots 183

6 . 1 .6 Specialized 2-D plots


There are many specialized graphics functions for 2-D plotting. They are used as

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:

are a creates a filled area plot ,


bar creates a bar graph,
barh creates a horizontal bar graph,
comet makes an animated 2-D plot,
compass creates arrow graph for complex numbers,
cont our makes contour plots,
contour£ makes filled contour plots,
errorbar plots a graph and puts error bars,
f e ather makes a feather plot ,
fill draws filled polygons of specified color,
fplot plots a function of a single variable,
hist makes histograms,
loglog creates plot with log scale on both the x-axis and the y-axis,
pareto makes pareto plots,
pcolor makes pseudocolor plot of a matrix,
pie creates a pie chart ,
plotyy makes a double y-axis plot ,
plotmatrix makes a scatter plot of a matrix,
p o l ar plots curves in polar coordinates,
quiver plots vector fields,
rose makes angled histograms,
s c at t e r creates a scatter plot,
semilogx makes semilog plot with log scale on the x- axis ,

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.

On the following pages, we show examples of these functions. The commands


shown in the middle column produce t he plots shown in the right c.olumn . There are
several ways you can use these gr ap hi c s functions and many of t hem take optional
arguments. The following examp les should give you a basic idea of how to use these
functions and what kind of plot to expect from them. For more information on any
of these functions, !';ee the on-line help.
184 Graphics

Function Example Script Output

f (t) = t sin t, 0 ::; t ::; l01r

fplot ( ' x . * s in ( x ) ' , [0 l O*pi] )


fplot
Note that the function to be
plotted must be written as
a function of x .

s em i l ogx
t = l inspace ( 0 , 2*pi , 200) ;
x exp ( - t ) ; y = t ;
=

semilogx ( x , y) , gri d

x = t, y e' , 0 ::; t ::;


oo'
= 21r
semilogy t = l inspace ( 0 , 2*pi , 20 0 ) ;
semilogy (t , exp ( t ) ) ,.

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

Function Example Script O utput


90 1 . 5

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

bar t l inspace (0 , 2*pi , 200) ;


r = sqrt ( abs ( 2 * s in ( 5 *t ) ) ) ;
y r . * s in (t) ;
bar ( t , y )
ax i s ( [O pi 0 inf ] ) ;

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

6.2 Using subplot for Multiple Graphs


If you want to make a few plots and place the plots si de by side ( not overlay ) , use
the subplot command to design your layout. The subpl ot command requires three
integer argum ent s :

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 .

6.3 3-D Plots


MATLAB p rovi des extensive facilities for visualizing 3-D data. In fact, the built-in
colormaps may be used to represent the fourth dimension. The facilities provided
include built-in functions for plotting space curves, wire-frame objects, and shaded
surfaces; generating contours automatically; displaying volumetric data; specifying
light sources; interpolating colors and shading; and even displaying images. Typing
he lp graph3d in the command window gives a list of functions available for general
3-D graphics. Here is a list of commonly used functions other than those from the
ez - st ab le, such as ezsurf , ezmesh, ezplot3 , discussed in Section 3 . 8 .
plot3 plots curves in space,
st em3 create::; discrete data plot with stems in 3-D ,
bar3 plots 3-D bar graph,
bar3h p l ots 3-D horizontal bar graph,
pie3 makes 3-D pie chart ,
c om e t3 makes animated 3-D line plot ,
f i ll3 draws filled 3-D polygons ,
contour3 makes 3-D contour plots,
qu iver3 dr aws vector fields in 3-D ,
s c att er3 makes scatter plots i n 3-D ,
mesh draws 3-D mesh surfaces ( wire-frame ) ,
me she draws 3-D mesh surfaces along with contours,
meshz draws 3-D me s h surfaces with reference plane curtains,
surf creat es 3-D surface p lots,
surfc creates 3 - D surface plots along with contours,
surf l creates 3-D surface plots with specified light source ,

t r imesh mesh plot with triangles ,


t r i surf s urface p lo t wit h t ri angle s ,
slice draws a volumetric surface with slices ,
waterfall creates a waterfall plot of 3-D d ata,
1 96 Graphics

[Function Example Script Output

\
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

Plot of four filled polygons


with three vertices each.

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 )

Plot o f 3-D contour lines of

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

Function Example Script Output

- 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

lxl :S: 5, IYI :S: 5

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

lxl :S: 5 , IYI :S: 5

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 .

You might also like