0% found this document useful (0 votes)
29 views

Lecture 3

The document discusses MATLAB graphing and plotting functions. It introduces the plot function for creating basic 2D line plots from data and describes how to customize line colors, styles, and markers. It also covers plotting multiple graphs, matrices, circles, and using functions like title, legend, and axis to label and format plots. Finally, it discusses more advanced plotting tools like subplots, logarithmic plots, 3D surface plots, and the stem function.

Uploaded by

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

Lecture 3

The document discusses MATLAB graphing and plotting functions. It introduces the plot function for creating basic 2D line plots from data and describes how to customize line colors, styles, and markers. It also covers plotting multiple graphs, matrices, circles, and using functions like title, legend, and axis to label and format plots. Finally, it discusses more advanced plotting tools like subplots, logarithmic plots, 3D surface plots, and the stem function.

Uploaded by

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

University of Dar es Salaam

ES171: Computer Aided


Drafting and Design (CADD)

Unit IV: Introduction to MATLAB


University of Dar es Salaam

LECTURE 3
 MATLAB Graphing
University of Dar es Salaam
PLOTTING

• The plot function returns graphs according to the input


arguments
• Allows customization of number of arguments, line patterns,
colors…
• MATLAB automatically selects appropriate axis ranges
• A new Figure window is created
University of Dar es Salaam
PLOTTING

• The plot function can be used in different ways


>> plot(data)
>> plot(xdata, ydata)
>> plot(data, ‘color style marker-’)
• Use doc plot or help plot to understand more on the plot
function
University of Dar es Salaam
2D LINE PLOT

Examples
• plot(y) produces a linear graph of the elements of y versus the indices
of the elements of y
y=linspace(0,10,10); plot(y)
• If two vectors are specified as arguments, plot(x,y) produces a graph
of y versus x. y and x are of equal length
x = linspace(0,2*pi); y=sin(x) ; plot(x,y)
University of Dar es Salaam
MULTIPLE PLOT
Plotting multiple graphs in
one call
Syntax: >> plot(x,y1,x,y2,x,y3)
Example:
University of Dar es Salaam
PLOT & HOLD • Try this

Plotting multiple graphs in


different calls
• Syntax: >> plot(x,y1)
hold on
>>plot(x,y2)
>> hold off
University of Dar es Salaam
LINE COLORS
• Line colors are specified by
predefined letters
Syntax >>plot(x,y, ‘color’)
• Example from previous plot
University of Dar es Salaam
LINE STYLES
• Line styles can also be changed
using predefined symbols
Syntax >>plot(x,y, ‘style’)
• Example from previous plot
University of Dar es Salaam
MARKER TYPES
• Marker types marks x and y
intersecting points using
predefined symbols
Syntax >>plot(x,y, ‘marker’)
University of Dar es Salaam

• Line colors, line styles and


markers can be used
together in one call
University of Dar es Salaam
Color Line style Marker type
. point
- solid o circle
b blue : dotted x x-mark
-. dashdot + plus
g green -- dashed * star
r red s square
d diamond
c cyan v triangle (down)
^ triangle (up)
< triangle (left)
m magenta > triangle (right)
p pentagram (special symbol)
y yellow h hexagram (special symbol)
k black
University of Dar es Salaam
Other properties that be specified

• LineWidth – specifies the width of the line in units of points


• MarkerEdgeColor – specifies the color of the marker or the edge color
for filled markers (circle, square, diamond, pentagram, hexagram,
triangles)
• MarkerFaceColor – specifies the color of the face of filled markers
• MarkerSize – specifies the size of the marker in units of points
University of Dar es Salaam
MATRIX PLOT
• Each column of a matrix is
treated as a different set of data
• Number of graph lines =
number of matrix columns
• Example:
>> R=rand(5,3).*5;
plot(R)
University of Dar es Salaam
PLOTTING A CIRCLE

• Specify y and x center points


• Specify the radius
• Use axis equal to use equal data units along each coordinate
direction.
University of Dar es Salaam
• Try this
• r = 2; (radius)
• xc = 3; yc = 3; (center
points)
• circ = linspace(0,2*pi);
• x = r*cos(circ) + xc;
• y = r*sin(circ) + yc;
• plot(x,y)
• axis equal
University of Dar es Salaam
OTHER FUNCTIONS

• Other functions that are helpful to create plots


• title – label the title of your graph
• legend – create legend, legend off – delete legend
• axis – control axis scaling and appearance
• xlabel – label the x axis
• ylabel – label the y axis
• grid, grid off
• Read more on Matlab doc or help function
University of Dar es Salaam

• Try this
• >> x = [0:0.1:2*pi];
• >> y = sin(x); z=cos(2*x)
• Plot the graph of y vs x
and hold
• Plot the graph of z vs x
• Inset graph title, legend
and label the axes.
• Grid on and grid off
University of Dar es Salaam
THE SUBPLOT FUNCTION

• The subplot function breaks the figure window into matrix of


small axes.
• Syntax subplot(m,n,p)
Return an m-by-n matrix of small axes, p-th axes for the current plot
University of Dar es Salaam
p=1
p=2 p=1 p=2
p=3 p=4
subplot(2,1,1) % p=1
subplot(2,2,1) % p=1
subplot(2,1,2) % p=2 subplot(2,2,2) % p=2
p=1 p=2
subplot(2,2,3) % p=3
subplot(1,2,1) % p=1

subplot(2,2,4) % p=4
subplot(1,2,2) % p=2
University of Dar es Salaam
EXAMPLES…
University of Dar es Salaam
EXAMPLES…
University of Dar es Salaam
EXAMPLES…
University of Dar es Salaam
LOGARITHMIC PLOTS

• semilogx:– plots x-axis logarithmic scale, y-axis in linear scale


• semilogy:– y-axis logarithmic scale , x-axis in linear scale
• loglog – logarithmic scales are used for both the X- and Y-
axes.
University of Dar es Salaam
THE STEM FUNCTION
• Plots the data sequence of y axis
at values specified by x axis
• Also known as sampling of data
University of Dar es Salaam
3D GRAPHS

The mesh plot Function


• Creates a 3D mesh surface
• Example:
>> z = peaks(25);
mesh(z)
Read more on mesh
function
University of Dar es Salaam
3D GRAPHS
The surf plot Function
• Creates a 3D color surface
• Example:
>> z = peaks(25);
surf(z)
Read more on surf function
Read more on peaks function
University of Dar es Salaam
Reading Assignment

1. An Introduction to Matlab Version 3.1 David F. Griffiths Univ. of


Dundee : Section 13 Plotting Functions
2. Matlab getting started - Matlab Primer R2016B – Chapter 4 – Basic
Plotting Functions
3. Mastering MATLAB (Hanselman & Littlefield) – Chapter 25 Two-
Dimensional Graphics

You might also like