0% found this document useful (0 votes)
43 views30 pages

Lecture 3 - Plotting

This document discusses various types of 2D and 3D plotting in MATLAB. It covers creating basic x-y plots from data, adding titles and labels, using different line styles and colors, creating subplots, histograms, function plots, and 3D surface and contour plots. It also discusses saving and editing plots.

Uploaded by

erick
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)
43 views30 pages

Lecture 3 - Plotting

This document discusses various types of 2D and 3D plotting in MATLAB. It covers creating basic x-y plots from data, adding titles and labels, using different line styles and colors, creating subplots, histograms, function plots, and 3D surface and contour plots. It also discusses saving and editing plots.

Uploaded by

erick
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/ 30

CPEG201L

PLOTTING
2-D Plotting

Time Distance
 x-y plot: (sec) (Ft)

 The independent variable is usually called x 0 0


2 0.33
 The dependent variable is usually called y
4 4.13
 Consider this x-y data:
6 6.29
 Time is the independent variable (x-axis)
8 6.85
 and distance is the dependent variable (y-axis) 10 11.19
12 13.19
%Define x and y and call the plot function
x = [0:2:18]; 14 13.96
y = [0 0.33 4.13 6.29 6.85 11.19 13.19 13.96 16.33 18.17]; 16 16.33
plot(x,y) 18 18.17
Titles and Labels

 Title
 To add an apostrophe to a title (or other annotation) you must enter the single
quote twice – otherwise MATLAB interprets the single apostrophe as the end of
the string.
 X axis label, Y axis label
 Often it is useful to add a grid

title(‘Razan''s lab experiment 1')


xlabel('time (sec)')
ylabel('distance (ft)')
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)
Plots with multiple lines (hold on)

 hold on
 Freezes the current plot, so that an
additional plot can be overlaid
 When you use this approach, the
additional line is drawn in red – the
default drawing color is blue
 To unfreeze the plot, use the hold off
command
Plots with multiple lines using one
command

 You can also create multiple lines on a single graph with one command.
 Each set of ordered pairs will produce a new line.
Plots with multiple lines with the same x
value

 Use alternating sets of ordered pairs


 Or group the y values into a matrix
Plot command with a single matrix

 If you use the plot command with a single matrix, MATLAB plots the values
versus the index number
 Usually, this type of data is plotted on a bar graph
 When plotted on an x-y grid, it is often called a line graph
Try plot(peaks(100))

 The peaks(100) function creates a 100x100 array of values. Since this is a


plot of a single variable, we get 100 different line plots.
Plots of Complex Arrays

 If the input to the plot command is a single array of complex numbers,


MATLAB plots the real component on the x-axis and the imaginary
component on the yaxis
Multiple arrays of complex numbers

 If you try to use two arrays of complex numbers in the plot function, the
imaginary components are ignored
Line, Color and Mark Style

Mark Style Indicator

point .
 You can change the appearance of
your plots by selecting: circle o

x-mark x
 line styles, color, mark styles
Color Indicator plus +
 If you don’t specify style, the default is
star *
used which is: line style – none, mark blue b
style – none, and color – blue green g
square s

 Specify your choices in a string. red r


diamond d

triangle down v
 For example Line Style Indicator cyan c
triangle up ^
 plot(x,y,':ok’) solid - magent m
triangle left <
a
 dotted line, circles, black dotted :
yellow y triangle right >
dash-dot -.
pentagram p
dashed -- black k
hexagram h
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])
Legends and Textbox
Improving your labels

 Use Greek letters in your labels by putting a backslash (\) before the name
of the letter.
 title(‘\alpha \beta \gamma’) creates the plot title α β γ

 Use curly brackets to create a superscript


 title(‘x^{2}’) → gives 𝒙𝟐

 Use underscore to create a subscript


 title(‘x_2’) → gives 𝒙𝟐
Subplots

 The subplot command allows you to subdivide the graphing window into a
grid of m rows and n columns.
 subplot(m,n,p)
 m = rows, n = columns, p = location.
 Example: subplot(2,2,1)
Peaks

-5 2
2
0 2
0
-2 -2
y x

3 4
Other Types of 2-D Plots

 Bar Graphs and Pie Charts


 Histograms
 X-Y graphs with two y axes
 Function Plots
Bar Graphs and Pie Charts

 MATLAB includes a whole


family of bar graphs and pie
charts
 bar(x) – vertical bar graph
 barh(x) – horizontal bar graph
 bar3(x) – 3-D vertical bar
graph
 bar3h(x) – 3-D horizontal bar
graph
 pie(x) – pie chart
 pie3(x) – 3-D pie chart
Histograms

 A histogram is a plot showing the distribution of a set of values

Defaults to 10 bins
X-Y Graphs with Two Y Axes

 Sometimes it is useful to overlay two x-y plots onto the same figure.
However, if the order of magnitude of the y values are quite different, it
may be difficult to see how the data behave.
 Scaling Depends on the largest value plotted.

 It is difficult to see how the blue line behaves, because the scale isn’t
appropriate.
X-Y Graphs with Two Y Axes - plotyy

 The plotyy function allows you to use two scales on a single graph
X-Y Graphs with Two Y Axes – Labels

 But how do you add the right axis label?


 Give the plot a name – also called a ‘handle’
Function Plots

 Function plots allow you to use a function as input to a plot command,


instead of a set of ordered pairs of x-y values
 fplot('sin(x)',[-2*pi,2*pi])
 sin(x): function input as a string
 [-2*pi,2*pi]: range of the independent variable – in this case x
Three-Dimensional Plotting

Line plots, Surface plots, Contour plots


Line Plots

 These plots require a set of order triples (x-y-z values) as input


 The z-axis is labeled the same way the x and y axes are labeled
 MATLAB uses a coordinate system consistent with the right-hand rule
Surface Plots

 Represent x-y-z data as a surface


 mesh – mesh plot
 surf – surface plot

 Both mesh and surf can be used to good effect with a single two-dimensional matrix
Surface Plots – mesh

 If x and y are unknown, the x and y coordinates are the matrix index numbers
 If we know the values of x and y that correspond to our z values, we can plot against
those values instead of the index numbers
Surface Plots – surf

 surf plots are similar to mesh plots


 they create a 3-D colored surface instead of an open mesh
 syntax is the same
Editing Plots from the Menu Bar

 In addition to controlling the way your plots look by


using MATLAB commands, you can also edit a plot
once you’ve created it using the menu bar
 Another demonstration function built into MATLAB is
sphere
 Once you’ve created a plot you can adjust it using
the menu bar
 In this picture the insert menu has been selected
 Notice you can use it to add labels, legends, a title and
other annotations
Saving your plots

 Save the figure from file → Save as →


You’ll be presented with several choices
of file format such as:
 MATLAB Figure
 JPEG Figure

You might also like