Appendix B

MATLAB’s Plotting Routines

The tables in this appendix, B-1 through B-5, list each of the plotting routines in MATLAB with a brief description and an example. To save space, some entries show a single encompassing example for multiple commands. An example may also use an ellipsis (…) to show the continuation of a pattern. To assist you, the code includes several functions to generate test matrices, for example, rand(), magic(), peaks(), cylinder(), ellipsoid(), and sphere(). It’s also important to note that MATLAB is always changing and that some functions, like ezpolar(), are eventually going to go away. After all, many of the other ez* functions have retired and are now sipping beverages on a sandy beach on a tropical island.

TABLE B-1 Basic Plotting Routines

Routine

Description

Example

comet

Just like plot, but comet animates the trajectory; it helps to have a larger vector to slow down the comet trace a tad

x=[0:2*pi/100:2*pi];

y=exp(-0.4*x).*sin(x);

plot(x,y); figure(2)

comet(x,y); figure(3)

ribbon(x,y);figure(4)

y2=100*exp(-0.4*x).*cos(x);

plotyy(x,y,x,y2)

plot

Plots data passed in by vectors

plotyy

Plots data where y values may differ greatly — it makes two y-axes

ribbon

Like plot, but it displays the data as 3D ribbons

fplot, fimplicit

Plots the expression or function

fplot(@(x) sin(x),[-2*pi 2*pi])

loglog

Plots data on a log scale on both x- and y-axes — y proportional to a power of x is straight on this plot

x=[0:2*pi/100:2*pi];

y=10*x.^pi;

loglog(x,y)

semilogx

X-axis data is on a log scale and the y-axis is on a linear scale — y linearly related to log(x) is straight on this plot

x=[0:2*pi/100:2*pi];

y=10*log(x)+pi;

semilogx(x,y);

semilogy

Y-axis data is on a log scale and x-axis linear — y proportional to exponential of x is straight on this plot

x=[0:2*pi/100:2*pi];

y=10*exp(pi*x);

semilogy(x,y);

TABLE B-2 Beyond Basics

Routine

Description

Example

area

Acts just like plot except for the fact that it fills in the area for you

x=[0:2*pi/100:2*pi];

y=exp(-0.4*x).*sin(x);

area(x,y)

bar

Creates a standard bar chart that can handle both grouping and stacking

x=[8,7,6;13,21,15;32,27,32];

bar(x); figure(2)

y=sum(x,2);

bar(y); figure(3)

bar(x,'stacked'); figure(4)

barh(x); figure(5)

bar3(x); figure(6)

bar3h(x)

barh

Just like bar except plot is horizontal

bar3

Adds a little 3D pizazz to a standard bar chart

bar3h

Creates a 3D horizontal bar chart

compass

Like polar, but compass shows data as vectors from origin

compass(rand(1,3)-0.5,…

rand(1,3)-0.5)

ezpolar

Creates a polar plot where the distance from origin vs. angle is plotted — argument is function expression

ezpolar('cos(2*x)^2');

fill

Fills polygons (vector inputs define vertices) with the specified color

y=sin([0:2*pi/5:2*pi])

x=cos([0:2*pi/5:2*pi])

fill(x,y,'g')

pie

Creates a standard pie chart

x=[2,4,6,8];

pie(x); figure(2)

pie3(x)

pie3

Adds some 3D pizazz to pie

polar

Creates a polar plot like ezpolar but does so by accepting vector arguments — x value corresponds to angle and y value to distance from origin

x=[0:2*pi/100:2*pi];

y=(exp(-0.1*x).*sin(x)).^2;

polar(x,y)

TABLE B-3 Statistical Plotting Routines

Routine

Description

Example

errorbar

Like scatter but adds error bars

x=[0:2*pi/100:2*pi];

y=10*x+pi+10*randn(1,101);

scatter(x,y); figure(2)

stem(x,y); figure(3)

errorbar(x,y,10*ones(1,101),ones(1,101))

figure(4);

hist(y); figure(5)

histogram(y)

histc(y,[-40:20:80])

histcounts(y,5)

hist, histogram

Creates a histogram — a bar chart showing the frequency of occurrence of data vs. value. These two routines handle the bins differently

scatter

Plots (x,y) data points

stem

Like scatter, but adds a line from the x axis to a data point

histc

Related to hist, but rather than making a plot, it makes a vector of counts

histc(y,[-40:20:80])

histcounts

Like histc except it creates n bins

histcounts(y,5)

stairs

Like scatter, but makes stairsteps when y values change

x=[0:2*pi/10:2*pi];

y=10*x+pi+10*randn(1,11);

stairs(x,y)

rose

A cross between polar and histogram; it displays frequency vs. angle

rose(randn(1,100),5)

pareto

A bar chart arranged with the highest bars first

histc(randn(1,100),[-4:1:4])

pareto(ans)

spy

A scatter plot of zeros in a matrix

mymat=rand(5);

mymat=(mymat>0.5).*mymat;

spy(mymat)

plotmatrix

A scatter plot of all permutations of columns of x and y

plotmatrix(magic(3),magic(3))

TABLE B-4 3D Graphics

Routine

Description

Example

contour

Creates a contour plot with matrix arguments

x=[-2*pi:4*pi/100:2*pi];

y=[-2*pi:4*pi/100:2*pi];z=cos(x)'*cos(y);

contour(x,y,z);title('contour');figure(2)

contourf(x,y,z);title('contourf');figure(3)

contour3(x,y,z);title('contour3');figure(4)

surf(x,y,z);title('surf');figure(5)

surface(x,y,z);title('surface');figure(6)

mesh(x,y,z);title('mesh');figure(7)

waterfall(x,y,z);title('waterfall');figure(8)

surfc(x,y,z);title('surfc');figure(9)

meshc(x,y,z);title('meshc');figure(10)

meshz(x,y,z);title('meshz');figure(11)

surfl(x,y,z);title('surfl');figure(12)

pcolor(z);title('surfl')

contourf

Same as contour, except contourf fills in the contours

contour3

Same as contour, except contour3 provides a 3D perspective

mesh

Creates a wireframe mesh surface

meshc

Wireframe mesh with contour

meshz

Wireframe mesh with a curtain around the plot

pcolor

Shows values of the matrix as colors

surfc

Same as surf, but with contour plot added

surf, surface

Creates a filled surface

surfl

Same as surf, except surfl simulates light and shadow

waterfall

Like mesh, but all column lines are omitted

fcontour

Plots the function as a contour

x = (-5:.1:5);

y = (-5:.1:5);

f = @(x,y) sin(x) + cos(y);

fcontour(f)

fill3

Like fill except in 3D; note that the fill area may not be coplanar

fill3([0,1,1,0],[0,0,1,0],[0,1,0,1],'g')

fmesh

Plots the function as a mesh

x = (-5:.1:5);

y = (-5:.1:5);

f = @(x,y) sin(x) + cos(y);

fmesh(f)

fplot3

Plots a series of three functions expressing x-, y-, and z-axes as a parametric curve

t = (-5:.1:5);

x=sin(t);

y=cos(t);

z=t;

xt = @(t) sin(t);

yt = @(t) cos(t);

zt = @(t) t;

fplot3(xt,yt,zt)

fsurf

Plots the function as a 3D surface

x = (-5:.1:5);

y = (-5:.1:5);

f = @(x,y) sin(x) + cos(y);

fsurf(f)

plot3

Like ezplot3, except it takes vector arguments

a=[-2*pi:4*pi/100:2*pi];

x=sin(a); y=cos(a);

z=sin(3*a/2);

plot3(x,y,z);

figure(2)

scatter3(x,y,z)

scatter3

Like plot3, but shows individual points

stem3

Stem plot of 3D data

stem3(rand(5))

surfnorm

Creates surface plot with normal vectors

[x,y,z]=peaks; % Test function

surfnorm(x,y,z)

TABLE B-5 Vector Fields

Routine

Description

Example

coneplot

Like quiver3, but coneplot shows velocity as cones

[x,y,z]=meshgrid([-5:2:3],[-3:2:3],[-3:2:3]);

r=sqrt(x.^2+y.^2+z.^2);

u=ones(4,5,4)+(10./r.^2).*cos(atan2(y,x)).*sin(acos(z./r));

v=(10./r.^2).*sin(atan2(y,x)).*sin(acos(z./r));

w=10.*z./r; quiver3(x,y,z,u,v,w);hold on;

streamribbon(x,y,z,u,v,w,-5,0,.1);figure(2)

coneplot(x,y,z,u,v,w,x,y,z);figure(3)

quiver3(x,y,z,u,v,w);hold on;

streamtube(x,y,z,u,v,w,-5,0,.1);

quiver3

Like quiver for 3D; the example adds a uniform velocity field to a 1/r2 velocity field

streamribbon

Like streamline, but shows ribbons

streamtube

Like streamline, but plots tubes (cylindrical 3D flow lines)

feather

Similar to compass, except it steps once for each element in x and y

feather(rand(1,3)-0.5,rand(1,3)-0.5)

quiver

Works like feather, except quiver plots vectors in an x-y plane

[x,y]=meshgrid([-5:5],[-5:5]);

u=ones(11)+(4./(sqrt(x.^2+y.^2)).*cos(atan2(y,x)));

v=(4./(sqrt(x.^2+y.^2)).*sin(atan2(y,x)));

v(6,6)=0; u(6,6)=0;

quiver(x,y,u,v); hold on

streamline(x,y,u,v,[-5,-5,-5],[-1,.01,1]); hold off

streamline

Plots line from the vector field; the example plots 2D streamline on the same plot as quiver

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.178.207