Lecture 25: Exploring data Two Dimensional Plots The plot The The xy plot is the most commonly used by engineers independent variable is usually called x dependent variable.

Download Report

Transcript Lecture 25: Exploring data Two Dimensional Plots The plot The The xy plot is the most commonly used by engineers independent variable is usually called x dependent variable.

Lecture 25: Exploring data
Two Dimensional Plots
The
plot
The
The
xy plot is the most commonly used
by engineers
independent variable is usually called x
dependent variable is usually called y
Define x and y and call the plot
function
Engineers always add …
Title
title(‘y = cos(x)’)
Single quotes are used.
X axis label, complete with units
xlabel(‘x-axis’)
Y axis label, complete with units
ylabel(‘y-axix’)
Often it is useful to add a grid
grid on
Creating multiple plots
MATLAB overwrites the figure window
every time you request a new plot
To open a new figure window use the
figure function – for example
figure(2)
Create multiple lines on a single
graph
Each set of
ordered pairs
will produce a
new line
If you want to create multiple plots, all
with the same x value you can…
Use alternating sets of
ordered pairs

plot(x,y1,x,y2,x,y3,x,y4)
Or group the y values into a
matrix


z=[y1;y2;y3;y4]
plot(x,z)
x = 0:pi/100:2*pi;
y1 = cos(x);
y2 = cos(x)*2;
y3 = cos(x)*4;
y4 = cos(x)*6;
Line, Color and Mark Style
You can change the appearance of your
plots by selecting user defined
 line styles
 mark styles
 color
Try using
help plot
for a list of available styles
Specify your choices in a string
For example
plot(x, y, ':ok')




strings are identified with single quotes
the : means use a dotted line
the o means use a circle to mark each point
the letter k indicates that the graph should be
drawn in black
 (b indicates blue)
Available choices
Table 5. 2 Line, Mark and Color Options
Line Type
Indicator
Point Type
Indicator
Color
Indicator
solid
-
point
.
blue
b
dotted
:
circle
o
green
g
dash-dot
-.
x-mark
x
red
r
dashed
--
plus
+
cyan
c
star
*
magenta
m
square
s
yellow
y
diamond
d
black
k
triangle down
v
triangle up
^
triangle left
<
triangle right
>
pentagram
p
hexagram
h
specify the drawing
parameters for
each line after the
ordered pairs that
define the line
Axis scaling
MATLAB automatically scales each plot
to completely fill the graph
If you want to specify a different axis –
use the axis command
axis([xmin,xmax,ymin,ymax])
axis([0, 11, 0, 200])
Annotating Your Plots
You can also add

Legends
legend(‘string1’, ‘string2’, etc)
 The legend shows a sample of the line, and
 Lists the string specified.

Textbox
text(x_coordinate, y_coordinate, ‘string’)
 The box is placed at the specified x and y coordinates, and
 Contains the string value specified.
Of course you should always add


title
axis labels
4
Subplots
The subplot command allows you to
subdivide the graphing window into a
grid of m rows and n columns
subplot(m,n,p)
rows
columns
location
subplot(2,2,1)
2 columns
Peaks
5
2
1
0
-5
2
2 rows
0
y
-2
3
-2
0
2
x
4
2 rows and
1 column
Saving your plots
Rerun your M-file to recreate a plot
Save the figure from the file menu using the
save as… option
 You’ll be presented with several choices of
file format such as
 jpeg
 emg (enhanced metafile) etc
Other Types of 2-D Plots
Polar Plots
Logarithmic Plots
Bar Graphs
Pie Charts
Histograms
X-Y graphs with 2 y axes
Function Plots
Three Dimensional Plotting
Line plots
Surface plots
Contour plots
x = [-2:0.1:2];
y = [-2:0.1:2];
[X, Y] = meshgrid(x, y);
Z = X.*exp(-X.^2 - Y.^2);
surf(X, Y, Z)
Practice Question
Q. How many times will “Winter is over!!” print out?
int main( void )
{
int x = 3;
int y = 1;
Solution: D
}
do {
printf(“Winter is over!!\n”);
if (x > y) {
x = 0;
A) It may never stop printing.
y = 2;
B) 0
}
C) 1
x++;
D) 2
} while (x < y);
E) 3