You are on page 1of 11

MATLAB GRAPHICS

by
Mohamed Hussein
compliments to
Prof. Michael Negnevitsky
Univerity of Tasmania

Lecture 2
Matlab Graphics
Creating

Simple Plots
Manipulating Plots

Creating Simple Plots


Plots are a powerful visual way to interpret
data.
Matlab has an extensive graphics
capabilities.

Case Study: y = sin(x)


Plot a sine function over one period:
y = sin(x) for 0 < x < 2
First we choose data point for the
independent variable x. This data forms the
horizontal axes of the plot.
Then the sine of each data point is found this provides the vertical axes of the plot.
Each pair {xn, yn} is then marked on a
suitable set of axes.

Case Study: y = sin(x) (cont.)

Matlab uses arrays to accomplish this task:


>> x = linspace(0,2*pi,30);
creates 30 points between 0 and 2.
>> y = sin(x);
finds the sine of the points in x.
>> plot(x,y)
generates the plot.

Case Study: y = sin(x) (cont.)


1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

The Matlab function plot automatically


chooses axis limits,
marks the individual data points, and
draws straight lines between them.

Options in the plot command allow us to


plot multiple data sets on the same axes,
use different line types (dotted and dashed),
mark just the data points without drawing
lines between them,
use different colors for curves,
place labels on the axes, a title on the top,
draw a grid at the tick marks.

>> z = cos(x);
>> plot(x,y,x,z)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

>> plot(x,y,x,y,'+')
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

Plots a sine twice, once with lines connecting the data points,
the second with the data points marked with the symbol +. 10

>> plot(y,z)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

Plots sine versus cosine.


11

>> plot(x,y,x,2*y.*z, '--')


1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

Plots 2sin(x)cos(x) using dashed lines.


12

>> grid
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

Places a grid a the tick marks of the current plot.


13

>> xlabel('independent variable X')


1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

3
4
independent variable X

Places an x-label on the current plot.


14

>> ylabel('dependent variables')


1
0.8
0.6

dependent variables

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

3
4
independent variable X

Places a y-label on the current plot.


15

>> title('2sin(x)cos(x) = sin(2x)')


2sin(x )cos(x ) = sin(2x)
1
0.8
0.6

dependent variables

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

3
4
independent variable X

Places a title on the current plot.


16

Colours and Line Styles


Symbol

Colour

Symbol

yellow

point

magenta

circle

cyan

x-mark

red

plus

green

star

blue

solid line

white

dotted line

black

-.

dash-dot line

--

dashed line

Line style

17

>> x = linspace(0,2*pi,30);
>> y = sin(x);
>> z = cos(x);
>> plot(x,y,'b:',x,z,'r--',x,y,'ko',x,z,'m+')
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

18

Manipulating Plots
We add lines to existing plots using the
command hold. Matlab does not remove
the existing curves when new plot
commands are issued. It adds new curves
to the current axes.
Setting hold off releases the current figure
window for new plots.

19

>> x = linspace(0,2*pi,30);
>> y = sin(x);
>> plot(x,y)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

First we plot a sine curve.


20

>> hold on
>> plot(x,z,'m')
>> hold off
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

Now we hold the plot and add a cosine curve.


21

You might also like