Day03 Plots Student 2020
Day03 Plots Student 2020
Copyright James Andrew Smith & others. Not for distribution outside York University.
2 2019
Outline
• The plot command
• Plotting a function
• with the plot command
• Plotting multiple graphs in the same plot
• Formatting a plot
• using commands
3
Copyright James Andrew Smith & others. Not for distribution outside York University.
2019
[Fill in here]
The plot Command
Used to create two-dimensional plots.
The simplest form is
When it is executed,
• a figure is created in the Figure window, which opens
automatically if not already open.
• The figure is a single curve with the x values on the horizontal
axis and y values on the vertical axis.
• The curve is made of straight-line segments that connect the
points whose coordinates are defined by corresponding
elements of x and y.
Copyright James Andrew Smith & others. Not for distribution outside York University.
4
2019
[Fill in here]
Example
Try the following in the command window or a script file:
x=[1 2 3 4 5 6 7 8];
y=[2 6 7 7 5 5 8 3];
plot(x,y) 8
7
Note that you can use any
variables instead of x and y. 6
4
a=[1:8];
b=[2 6 7 7 5 5 8 3]; 3
plot(a,b) 2
1 2 3 4 5 6 7 8
Copyright James Andrew Smith & others. Not for distribution outside York University.
5
2019
[Fill in here]
The plot Command
By default, the plot uses solid blue lines. But you can make
changes using additional optional arguments in plot:
plot(x, y, 'line specifiers')
Copyright James Andrew Smith & others. Not for distribution outside York University.
7 2019
[Fill in here]
Line Style Specifier
Copyright James Andrew Smith & others. Not for distribution outside York University.
8 2019
[Fill in here]
Marker Type Specifier
Copyright James Andrew Smith & others. Not for distribution outside York University.
10 2019
[Fill in here]
Solution to Exercise 1
year=[1988:1994];
sales=[8 12 20 22 18 24 27];
shg
28
26
Optional: 24
22
the shg command 20
brings the Figure 18
window to the 16
front 14
12
10
8
1988 1989 1990 1991 1992 1993 1994
Copyright James Andrew Smith & others. Not for distribution outside York University.
11 2019
More on the plot Command
You can specify the thickness of the line, the size of the
marker and the colors of the marker’s edge line and fill.
plot(x,y, 'line specifiers','PropertyName',PropertyValue)
Copyright James Andrew Smith & others. Not for distribution outside York University.
13 2019
Outline
• The plot command
• Plotting a function
• with the plot command
• Plotting multiple graphs in the same plot
• Formatting a plot
• using commands
Copyright James Andrew Smith & others. Not for distribution outside York University.
14 2019
[Fill in here]
Plotting a Function
In many situations, there is a need to plot a function.
% Plot y as a function of x
Copyright James Andrew Smith & others. Not for distribution outside York University.
15 2019
[Fill in here]
Exercise 2
Plot the sin(x) function for the value of x from 0 to 4p.
Copyright James Andrew Smith & others. Not for distribution outside York University.
16 2019
Outline
• The plot command
• Plotting a function
• with the plot command
• Plotting multiple graphs in the same plot
• Formatting a plot
• using commands
Copyright James Andrew Smith & others. Not for distribution outside York University.
17 2019
Plotting Multiple Graphs in the Same Plot
Sometimes, we need to make two or more graphs in one
plot 30
data1
data2
25
20
15
10
5
1988 1989 1990 1991 1992 1993 1994
Line specifiers
Copyright James Andrew Smith & others. Not for distribution outside York University.
19 2019
Exercise 5
Extending Exercise 1, plot the sales amounts vs. year for two
stores in one plot. One plot uses dashed red line with circle
marker and the other uses blue dotted line with diamond
marker
Copyright James Andrew Smith & others. Not for distribution outside York University.
20 2019
[Fill in here]
Solution 1 to Exercise 5
year=[1988:1994];
sales1=[8 12 20 22 18 24 27];
sales2=[5 16 18 16 25 19 18];
shg
Copyright James Andrew Smith & others. Not for distribution outside York University.
21 2019
[Fill in here]
Make Multiple Graphs in the Same Plot with
hold on/off commands
Usage:
plot(x, y, 'line specifiers')
hold on
plot(u, v, 'line specifiers')
plot(t, h, 'line specifiers')
hold off
•The hold on command keeps the Figure Window with the first
plot open.
•The second and third plots after hold on create two more
graphs that are added to that figure.
•The hold off command stops this “graph-adding” process. The
plot command after hold off will erase the previous
22 plot.
[Fill in here]
Solution 2 to Exercise 5
year=[1988:1994];
sales1=[8 12 20 22 18 24 27];
sales2=[5 16 18 16 25 19 18];
hold on
hold off
shg
Copyright James Andrew Smith & others. Not for distribution outside York University.
23 2019
Outline
• The plot command
• Plotting a function
• with the plot command
• Plotting multiple graphs in the same plot
• Formatting a plot
• using commands
Copyright James Andrew Smith & others. Not for distribution outside York University.
24 2019
Formatting a Plot
When making a plot, we often need to specify:
• axis labels
• the title of the plot
• legend
• text labels
Copyright James Andrew Smith & others. Not for distribution outside York University.
25 2019
The xlabel and ylabel Commands
Format:
xlabel('text to be shown under x axis')
ylabel('text to be shown beside y axis')
year=[1988:1994];
sales1=[8 12 20 22 18 24 27];
sales2=[5 16 18 16 25 19 18];
plot(year, sales1, 'r--o', year, sales2, ':d')
xlabel('Year')
ylabel('Sales Amount in Millions')
shg
Copyright James Andrew Smith & others. Not for distribution outside York University.
26 2019
[Fill in here]
The title Command
Format: