0% found this document useful (0 votes)
7 views27 pages

Day03 Plots Student 2020

The document serves as an introduction to plotting in the EECS 1011 course, focusing on the use of the plot command to create two-dimensional graphs in MATLAB. It covers various aspects of plotting, including formatting, plotting functions, and creating multiple graphs in a single plot. Additionally, it provides exercises and examples to reinforce the concepts learned.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views27 pages

Day03 Plots Student 2020

The document serves as an introduction to plotting in the EECS 1011 course, focusing on the use of the plot command to create two-dimensional graphs in MATLAB. It covers various aspects of plotting, including formatting, plotting functions, and creating multiple graphs in a single plot. Additionally, it provides exercises and examples to reinforce the concepts learned.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to Plotting

EECS 1011 Class Session #3


Original material by Prof. Aijun An for EECS 1570
Adapted for EECS 1011 by Prof. James Smith
Copyright James Andrew Smith & others. Not for distribution outside York University.
1 2019
A Reminder: What is EECS 1011?
} Computational Thinking Through Mechatronics
This course provides a first exposure to programming; teaches a set of
soft computing skills; and demonstrates how computational thinking is
used to solve problems in engineering. It uses an interactive computing
environment to expose the underlying concepts and experiential labs to
implement them. Two lecture hours and three lab hours weekly.

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

The following is the same as


above: 5

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')

vector vector (Optional) Specify the


type and color of the
line and markers
Example:
plot(x,y,'r') % red solid line

plot(x,y,'g:') % green dotted line


plot(x,y,'r--*') % red dashed line with * markers on points
plot(x,y,'g:d') % green dotted line with diamond markers
plot(x,y,'*') % points marked with *, no line in between
Copyright James Andrew Smith & others. Not for distribution outside York University.
6 2019
[Fill in here]
Line Color Specifier

Line Color Specifier


Red r
Green g
Blue b
Cyan c
Magenta m
Yellow y
Black
White w

Copyright James Andrew Smith & others. Not for distribution outside York University.
7 2019
[Fill in here]
Line Style Specifier

Line Style Specifier


solid (default) -
dashed --
dotted :
dash-dot

Copyright James Andrew Smith & others. Not for distribution outside York University.
8 2019
[Fill in here]
Marker Type Specifier

Marker Type Specifier


plus sign +
circle o
asterisk *
point .
cross x
square s
diamond d
five-pointed star p

You can use “ ” to get more types.


Copyright James Andrew Smith & others. Not for distribution outside York University.
9 2019
Exercise 1
Given the information in the following table, plot the sales
amount against year in a figure using dashed red line and
circle marker.
Year 1988 1989 1990 1991 1992 1993 1994
Sales 8 12 20 22 18 24 27
(millions )

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)

vector vector (Optional) (Optional) specify the line


Specify the type width, marker’s size, mark’s
and color of the edge and fill colors
line and markers

Try and see the difference: By default, LineWidth is 0.5


x=[1:8]; marker size is 6,
y=[2 6 7 7 5 5 8 3];
plot(x,y,'r--o', 'LineWidth', 2, 'markersize', 12)
plot(x,y,'r--o','LineWidth',2,'markersize',12,
'markerfacecolor','y')
plot(x, y, 'r--o', 'LineWidth', 2, 'markersize',
12
12,
'markerfacecolor', 'y', 'markeredgecolor', 'g')
Property Name and Property Value

Property Name Description Property Value


LineWidth The width of the line A number in units of
(or linewidth) points (default: 0.5)
MarkerSize The size of the marker A number in units of
(or markersize) points (default: 6)
MarkerEdgeColor The color of the Color specifier, typed
(or markeredgecolor) marker’s edge line as a string
MarkerFaceColor The color of the Color specifier, typed
(or markerfacecolor) filling of the marker as a string

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.

To plot a function with plot:

% create vector x with the domain of the function

y=exp(x); % create vector y with the function value at each x

% 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.

x=linspace(0, 4*pi, 100);


y=sin(x);

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

Two ways to do it:


• Use the plot command
• Use the hold on and hold off commands
Copyright James Andrew Smith & others. Not for distribution outside York University.
18 2019
Make Multiple Graphs in the Same Plot with
the plot Command
The command:
plot(x,y,u,v,t,h)
• This creates three graphs: y vs. x, v vs. u, and h vs. t, all in
the same plot.
• The vectors of each pair must be of the same length.
• MATLAB plots the graphs in different colors
• You can change the line colors and styles and add markers:

plot(x,y, 'r--o', u,v, 'g:d', t,h, 'b-*')

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

Year 1988 1989 1990 1991 1992 1993 1994


Sales in Store 1 8 12 20 22 18 24 27
(millions)
Sales in Store 2 5 16 18 16 25 19 18
(millions)

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

Two ways to specify them in MATLAB:


• Using the following commands:
xlabel, ylabel, title, legend, text
• Using the interactive plot editor in the Figure Window

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')

Add the labels to the sales plot:

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:

Add a title to the sales plot:


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')
title('Yearly Sales Amounts of Store 1 and Store 2')
shg
Copyright James Andrew Smith & others. Not for distribution outside York University.
27 2019

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy