You are on page 1of 45

See last slide for

licensing details

DAY 5
BASIC 2D PLOTS
MODIFYING PLOT APPEARANCE
MULTIPLE PLOTS
ADJUSTING AXES
SPECIAL 2D PLOTS
3D PLOTS
ECE105 – Cody Anderson
Basic plotting functions
ezplot()
fplot()
plot()
ezplot and fplot

 fplot & ezplot are used to quickly


plot a symbolic equation
 this differs from plot, which
plots discrete data points

 Define the equation in quotes and


set the interval in brackets

 fplot is preferred over ezplot


 plots more points where line is
changing the most
 can use fancy text
plot(x, y)

This function only plots


discrete data points!
(i.e. a set of ordered pairs)

Need a plot? Start by getting


two vectors of the same length
Adjusting resolution (step size)
axis labels
 ‘\bf’ is shortcut to make
font boldface
 (…,‘FontSize’,value) sizes
the font

 Increase the font size for


presentation
 Always include units!

 You can also put a title


above your plot using the
function: title()
Text modification shortcuts
Text Appearance Special Characters
 \bf — boldface font  \delta — ∂
 \it — italic font  \Delta — ∆
 \rm — normal font  \pi — π
 ^{} — superscript  \approx — ≈
 _{} — subscript  \leq — ≤

This is a very short list. Plenty more options at:


https://www.mathworks.com/help/matlab/ref/xlabel.html
Example of text modification
Text modification name-value pairs
Name Description Example of Paired Value
‘FontSize’ Set the font size 18
‘FontName’ Set the font ‘Cambria’
‘FontWeight’ Make bold or unbold ‘bold'
‘Color’ Set text color [0 1 0.5]
‘EdgeColor’ Set color of text box [0 1 0.5]
‘BackgroundColor’ Set color behind text [0 1 0.5]
‘LineStyle’ Set line style of text box ‘-.’
‘LineWidth’ Set line width of text box 2
‘Position’ Set location of text [0.4 0.6]
‘Rotation’ Set rotation of text -45
‘HorizontalAlignment’ Set text alignment ‘center’

Again, more options at: https://www.mathworks.com/help/matlab/ref/xlabel.html


Example with name-value pairs

RGB triplet. This


example mixes:
• 100% red
• 20% green
• 0% blue

xlabel('\bf Radius [cm]', 'FontSize', 18, 'FontName', 'Arial', 'EdgeColor', [1 0.2 0])
“y vs. x”
The phrasing used when describing a
plot tells us which is the dependent
and independent variable:

“Area vs. Radius”

Dependent Independent
variable (y) variable (x)
comes first comes last

However, in MATLAB the syntax of the function is plot(x, y)


Modifying Plot Appearance
Who wore it better?
 The default plot format is generally  Improvements can be made
not good for presenting your work  Larger labels
 Small labels  Discrete scatter points
 Thin solid blue line  Adjusted color and size of marker
The 3 shorthand modifiers • Markers as *
• Dash-dot line
• Red color

plot(x, y) plot(x, y, ‘*-.r’)


Options for shorthand modifiers
Modifier Color Modifier Marker Type  These modifiers may be
r Red + Plus sign in any order within the
g Green o Circle single quotes
b Blue * Asterisk  Both of the following
c Cyan . Dot would appear the same:
m Magenta x Cross  ‘g--+’
y Yellow s Square  ‘+g--’
k Black d Diamond
w White ^ Up-triangle  If Line Style is left out,
> Right-pointing triangle then only markers
Modifier Line Style < Left-pointing triangle appear on plot
- Solid line (default) p Five-pointed star  If Marker Type is left
-- Dashed line (pentagram) out, then only a line
: Dotted line h Six-pointed star appears on plot
-. Dash-dot line (hexagram)
Name-Value Pairs for more options
 LineWidth — Specifies the width (in points) of the line.
 MarkerEdgeColor — Specifies the color of the marker or the
edge color for filled markers*
 MarkerFaceColor — Specifies the color of the face of filled
markers*
 MarkerSize — Specifies the size of the marker in points (must be
greater than 0).
*colors can be defined by abbreviation (e.g. ‘r’ ) OR by an RGB triplet (e.g. [1 0 0] )

More options and details at:


http://www.mathworks.com/help/matlab/ref/plot.html#namevaluepairarguments
Name-Value Pairs Example
plot(x,y,'kh--','MarkerSize',22,'MarkerFaceColor',[1 0 .5],'LineWidth',2)
Multiple plots
plot(x1,y1,x2,y2)
legend()
hold
subplot(rows, cols, loc)
Multiple plots:
plot(x1,y1,x2,y2)
legend()
Multiple plots:
hold on
legend adjustments
Location reads
subplot(rows, cols, loc) left-to-right, then
top-to-bottom
Adjusting axes
xlim, ylim
axis
semilogx, semilogy
loglog
xlim 2. Apply ylim
ylim

3. Apply xlim

1. Original plot
Some syntaxes for axis
 axis( [xmin, xmax, ymin, ymax] )
Set the limits for both x- and y-axes in one command

 axis equal
Set the axis limits equal for x and y

 axis square
Make the axis box square

 axis off
Toggle off the axis labeling and tick marks
Example of axis vector
Example of axis off
The need for log-scale axes
 Most often, we use linear axes
 Add a consistent amount
between tick marks
 Sometimes, a logarithmic axis
is useful
 Multiply a consistent amount
(usually 10 or e) between
tick marks
Huge values in later data
make it impossible to view
 When use logarithmic? differences in early data
 Modeling an exponential
relationship
 Data extends across multiple
orders of magnitude
log-scale axes

With the log-scale y-axis:  Each tick mark is 100 times the previous tick mark
 We can see the differences in the early data
Axis scale functions
 plot(x,y) – x- and y-axes are both linear

 semilogy(x,y) – x-axis is linear; y-axis is logarithmic

 semilogx(x,y) – x-axis is logarithmic; y-axis is linear

 loglog(x,y) – x- and y-axes are both logarithmic


Special 2D plots
polar
stem, stair
bar, barh
pie
compass
hist
Polar

Source: "File:Polar rectangular.svg" by


Pirateer is licensed under Public Domain
Given data
Stem, Stairs
Given data
Bar, Barh
Pie
Compass
Histograms

Indicates that 15
evenly spaced bins
will be used

Indicates than bins


go from -3 to +3 in
steps of 0.1
3D Plots
Line plots: plot3
Surface plots: meshgrid, surf, mesh, contour

MATLAB plots discretely


plot3 -- 3D line plot

Useful for parametric equations


(one independent variable and
two dependent variables)
3d surface plots Useful for two independent variables
and one dependent variable

 In all of these plots, Z is the dependent variable and X and Y are


the independent variables. One value of Z corresponds to every
combination of values in X and Y
 mesh(X,Y,Z) —mesh or wireframe plot
 surf(X,Y,Z) —surface plot
 contour(X,Y,Z) — contour plot
3d plots are from ordered triplets
 MATLAB plots discretely, so 3d plots need ordered triplets
 Each value in Z is computed from input values of X and Y
 To create all Z values, we need all combinations of X and Y

Plot of Z = X + Y

X=3
X=2 Y=2
Y=8 Z=5
Z = 10
meshgrid
 meshgrid creates all combinations of x and y
 Returns square grid coordinates of size length(x) by length(y)

x vector, repeated across rows y vector, repeated across columns


meshgrid

With x_grid and y_grid defined,


a simple calculation can be used
to compute z.
Now, we have a set of (x,y,z)
coordinates to plot
Contour plot

[x, y] = meshgrid(-4:0.2:4);
z = exp(-0.5*(x.^2+y.^2));
contour(x, y, z)
colorbar
Surface plot
surf(x,y,z)

Mesh plot
mesh(x,y,z)
Except where otherwise noted, this work is licensed under the Creative
Commons Attribution-NonCommercial 4.0 International License.

To view a copy of this license


• visit http://creativecommons.org/licenses/by-nc/4.0/
• or send a letter to Creative Commons, PO Box 1866, Mountain View,
CA 94042, USA.

Creative Commons and the double C in a circle are registered trademarks


of Creative Commons in the United States and other countries. Third party
marks and brands are the property of their respective holders.

You might also like