ENGR 108
Introduction to Computing
for Engineers and
Scientists
LECTURE 4: GRAPHING
Objectives
After this lecture, students will be able to:
◦ Create two-dimensional plots
◦ Customize and label plots
◦ Divide the figure window into subplots
◦ Create special types of 2D plots
◦ Create 3D space-curve and surface plots
◦ Edit plots with the interactive tools
2
Making a plot
>> x = 1:10;
>> y = x.^2;
>> plot(x, y)
plot(x, y) takes the two
vectors x and y and plots the
coordinate pairs (x(1),
y(1)), (x(2), y(2)), … in
the figure window
3
An example graph
>> x = 0 : 1 : 10;
>> y = sin(x);
>> plot(x, y)
A smoother graph
>> x = 0 : 0.1 : 10;
>> y = sin(x);
>> plot(x, y)
Another way to make x
>> x =
linspace(0,10,100);
>> y = sin(x);
>> plot(x, y)
The linspace(x, y, n)
function makes a list of n values
between x and y
Adding labels and
annotations
◦ Label the x-axis with xlabel('text')
◦ Label the y-axis with ylabel('text')
◦ Add a title at the top with title('text')
◦ Add a label anywhere with gtext('text'), then clicking on the graph
◦ To add text at coordinate (x, y), use the command text(x, y,
'text')
◦ grid adds a dashed grid to the plot window
Changing the plot line
style (LineSpec)
plot(x, y, ‘linemarkercolor’)
Symbol Line Symbol Marker Symbol Color
- Solid + Plus r Red
-- Dashed o Circle g Green
: Dotted * Star b Blue
-. Dash-dot . Point c Cyan
None x Cross m Magenta
s Square y Yellow
d Diamond k Black
None w White
Using LineSpec with
plots
plot(x, y, ‘--og’) plot(x, y, ‘*b’)
Multiple figure windows
figure opens a new figure window, which all further plot commands
will draw inside
figure(n) will open a new figure window named "Figure n", or
switch to drawing in that window if it's already open
10
Multiple plots on the
same figure window
◦ Add another set of inputs for each additional graph
◦ plot(x1, y1, ‘linespec’, x2, y2, ‘linespec’, …)
◦ By default, this makes each plot a different color
◦ Plot one graph, type hold, then plot a second graph
◦ hold makes the next plot add to rather than replace the current graph
◦ plot(x1, y1, ‘linespec’)
hold
plot(x2, y2, ‘linespec’)
◦ By default, this makes each plot the same color
Modifying the plotted
range
The axis command changes the minimum and maximum values for
each of the axes. You can use this to zoom in on a specific part of a plot
◦ axis([xmin, xmax, ymin, ymax])
◦ axis tight fits the axes exactly around the data
Modifying the plotted
range
x=linspace(-
10,10,200);
y=x.^3-3*x.^2-
28*x+60;
plot(x,y)
Modifying the plotted
range
x=linspace(-
10,10,200);
y=x.^3-3*x.^2-
28*x+60;
plot(x,y)
axis([-10,10,-
100,100])
Adding a legend
A legend is a key to which plot
corresponds with which function
or data
Adding a legend
The legend function’s inputs are
legend(‘name 1’, ‘name 2’, … [‘Location’, ‘place’])
For example, if you have three sets of data, and you want the legend to be in the
bottom right of the graph, the command is
legend(‘Set 1’,‘Set 2’,‘Set 3’,‘Location’,‘SouthEast’)
Place options:
◦ North, East, South, West,
NorthEast, NorthWest, SouthEast, SouthWest
◦ Add Outside to put the legend outside the plot box
◦ Best looks for a location that won’t overlap the data
Example
17
Example
Cannon range
% Define the constants 1200
g = 9.8; 50 m/s
100 m/s
v1 = 50; 1000
v2 = 100;
% Define the angle vector
800
angle = 0 : 0.05 : pi/2;
Range (m)
% Calculate the range
600
R1 = v1^2/g*sin(2*angle);
R2 = v2^2/g*sin(2*angle);
400
% Plot the results
plot(angle,R1,angle,R2,':')
200
title('Cannon range')
xlabel('Cannon angle')
0
ylabel('Range (m)') 0 0.5 1 1.5 2
Cannon angle
legend('50 m/s', '100 m/s')
18
Subplots
c=3
subplot(r, c, n) divides
the current figure window into a n=1 n=2 n=3
grid of r rows and c columns, and
directs all the following plotting
commands to the nth sub- r=3 n=4 n=5 n=6
window
n=7 n=8 n=9
19
Subplots
x = -10:0.2:10;
y1 = x.^3;
y2 = abs(x);
y3 = floor(x);
y4 = 1./x;
subplot(2, 2, 1)
plot(x, y1, 'r')
subplot(2, 2, 2)
plot(x, y2, 'b')
subplot(2, 2, 3)
plot(x, y3, 'g')
subplot(2, 2, 4)
plot(x, y4, 'k')
20
Complex subplots
A subplot can cover multiple
subplot cells:
subplot(3, 3, [1,2,4,5])
plot(x, y1, 'r')
subplot(3, 3, [3,6])
plot(x, y2, 'b')
subplot(3, 3, [7:9])
plot(x, y3, 'g')
21
Polar plots
Up to this point, we have been
making graphs in the rectangular
coordinate system
Another coordinate system is
the polar system, where
coordinates are determined by
their distance from the origin and
the angle from horizontal
Polar plots
polar(theta,r) plots
the polar coordinates on a
special radial grid
Example:
t=linspace(0,2*pi,200);
r=2*sin(2*t);
polar(t,r)
Bar and pie charts
x = [1, 2, 5, 4, 8];
y = [8, 4, 5, 2, 1];
subplot(2,2,1)
bar(x)
subplot(2,2,2)
bar([x; y])
subplot(2,2,3)
bar3([x;y])
subplot(2,2,4)
pie(x)
24
Three-dimensional plots
SPACE CURVES SURFACES
25
Space curves
Space curve
Types of 3D surfaces
mesh(x,y,z) surf(x,y,z)
contour(x,y,z) surfc(x,y,z)
28
Surfaces
A simple example
Customizing 3D plots
zlabel works just like xlabel and ylabel
grid, legend and subplot work in 3D
axis equal sets all three axes to the same scale
box on draws a box around the plot volume
view(az,el) rotates the plot volume around the z axis by az
degrees, and tilts the volume towards you by el degrees
Types of shading
shading faceted shading interp shading flat
32
Colormaps for 3D plots
To change the color of a 3D
surface plot, use
colormap(name), where
name is one of the available
styles, all in lowercase
33
Publishing a script with
plots
When you publish a script with plot commands, by default it makes an
HTML file with separate image files for each plot
To share a published report with plots, you have two options:
◦ Outside of MATLAB, put the HTML files and image files into a .zip file
◦ In the script editor, go to Publish Configuration and change the file type to
one that embeds images, like DOC or PDF
34
Publishing a script with
plots
35
Next week
◦ Linear algebra and systems of simultaneous equations
36