You are on page 1of 13

November 30, 2009

2-Dimensional Plots
The most common plot is the x-y plot
The data is in vectors (1-dimensional matrices) called
x and y
x – independent variable
y – dependent variable
Plotting Example Data
x = [1:10];
y = [58.5, 63.8, 64.2, 67.3, 71.5, 88.3, 90.1, 90.6,
89.5, 90.4];

plot(x,y) % ind vs. dep. var


title(‘Laboratory Experiment 1’) % good practice
xlabel(‘Trial’)
ylabel(‘Distance, ft’) % units = good
grid on % optional

 This will automatically open a window called “Figure 1”


Plotting with M-files
After you request a plot inside an M-file, MATLAB
will automatically continue executing commands
which follow after “plot” command
If you attempt a second “plot”, the initial graph will
be overwritten
Two ways to solve this:
pause (or pause(n)) stops until any key is pressed
figure allows you to open a new figure window:
 figure(2) opens a new window named “Figure 2”
Using “hold on”
You can layer plots on top of one another using the
“hold on” command:

t = 0:pi/100:2*pi;
y1 = cos(t*4);
plot(t,y1)
y2 = sin(t);
hold on;
plot(t,y2)

Plots will continue to layer until until you say “hold off”
Plotting Multiple Lines
Another way to create multiple lines is to request both
lines in a single “plot” command:
 plot(X, Y, W, Z) % input is alternating
% x and y vectors
 plot(x, y1, x, y2) % from previous example

Using this technique is the same as using the “hold on”


method, with one big difference:
With “hold on”, each line is a separate “plot” command,
and all lines will be blue
Using one “plot” command recognizes the separate x-y
pairs, and each pair (line) will be a different color
Using a matrix vs. vectors
From the previous example:
 plot(x, y1, x, y2) % from previous example

Can also do this:


Y = [y1, y2]
plot(x, Y)

Both of these create the same plot, with each line a


different color. A line is plotted for each row (vector)
in the matrix
Line, Color, and Mark Style
Use “help plot” to discover the choices available to you
See Table 4.1 on page 107 for details

Example:
plot(x,y,‘:ok’,x,y*2,‘--xr’,x,y/2,‘-b’)

:ok – dotted, circle, black


--xr – dashed, x-mark, red
-b – solid, blue
Scaling and Annotating Plots
Please see page 108, sections 4.1.3 and 4.1.4 for
information on axes scaling and annotating plots
Examples for annotation:
 legend(‘string1’, ‘string2’, etc)
 legend(‘line1’, ‘line2’, line3’)

 text(x_coordinate, y_coordinate, ‘string’)


 text(1,100,‘Label plots with the text command’)

Results are shown in Figure 4.9, page 109


Other Graph Types
polar(theta, rho)
semilogx(x, y)
semilogy(x, y)
loglog(x, y)
bar(x)
barh(x)
bar3(x)
barh3(x)
pie(x)
pie3(x)
hist(x)
Subplots
clf % clf clears the parameters
x = -2*pi:.05:2*pi; % of the current figure
subplot(2,3,1)
plot(x, sin(x))
title(‘1 - sin(x)’);

subplot(2,3,2) subplot(2,3,5)
plot(x, cos(x)) plot(x, sqrt(x))
title(‘2 - cos(x)’); title(‘5 - sqrt(x)’);

subplot(2,3,3) subplot(2,3,6)
plot(x, tan(x)) plot(x, exp(x))
title(‘3 - tan(x)’); title(‘6 - exp(x)’);

subplot(2,3,4)
plot(x, x.^2)
title(‘4 – x^2’);
3-Dimensional Plots
The “plot3” function is similar to “plot”
The data is in vectors (1-dimensional matrices) called
x, y, and z
plot3(x, y, z)

mesh(z) or mesh(x, y, z) create a connected “mesh” of


points in 3-D space (see Figure 4.23)

surf creates a colored 3-D surface instead of the mesh


(but the basic structure is the same)
Questions?
Keep up with the reading!
Lab on Wednesday

You might also like