Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

2D & 3D plotting

Tan Do

Vietnamese-German University

Lecture 4

Tan Do (VGU) Introduction to MATLAB Lecture 4 1 / 25


Content

In this lecture

2D plotting: plot and subplot commands; plotting polynomials


and arbitrary functions; data markers, line types and colors; curve and
data labeling.
3D plotting: plot3, mesh and surf commands

Tan Do (VGU) Introduction to MATLAB Lecture 4 2 / 25


2D plotting

Plotting terminology

Figure: 2D plotting nomenclature


Tan Do (VGU) Introduction to MATLAB Lecture 4 3 / 25
2D plotting

plot function

plot(x,y) plots x data against y data.

xlablel(’text’) and ylabel(’text’) label x-axis and y -axis.

title(’text’) puts title at the top of the plot.

xlabel, ylabel and title must be placed after the plot


command.

Tick-marks are automatic. This is called autoscaling.

Tan Do (VGU) Introduction to MATLAB Lecture 4 4 / 25


2D plotting

Requirements for a correct plot

1 Label each axis with an appropriate name together with its unit.

2 tick-marks should be easy to read (e.g., 1, 0.1 or 0.2 rather than 0.13, 1.35).

3 When plotting more than one curve or data set, use dierent line types, data
symbols and legend labels to distinguish them.

4 Use a meaningful title.

5 Use data symbols for discrete data sets. Do not use data symbols for smooth
curves.

6 Generate enough points to obtain a smooth curve.

7 Do not generate a misleading plot.


Tan Do (VGU) Introduction to MATLAB Lecture 4 5 / 25
2D plotting

The grid command displays gridlines corresponding the tick-marks.

(a) Plot with no grid (b) Plot with grid on


The axis command chooses axes limits for the plot.
axis([xmin xmax ymin ymax])
Code for Figure (b)
x = -pi:0.01:pi;
y = sin(x);
plot(x,y)
axis([-pi pi -1.5 1.5])
grid
Tan Do (VGU) Introduction to MATLAB Lecture 4 6 / 25
2D plotting

fplot functions
A very powerful method for plotting function is to use fplot:

fplot(@function, [xmin xmax])

or

fplot(@function, [xmin xmax ymin ymax])

Example Plot f (x) = cos(tan(x)) − tan(sin(x)) on [1, 2].


Code 1
f = @(x) cos(tan(x)) - tan(sin(x));
fplot(f,[1,2])

Code 2
x = 1:0.01:2;
f = cos(tan(x)) - tan(sin(x));
Tan Do (VGU)
plot(x,f) Introduction to MATLAB Lecture 4 7 / 25
2D plotting

Subplots
We put plots next to each other to compare data. subplot function is
used to do so:
subplot(m,n,p)
This generates m × n panels with m rows and n columns, enumerated rst
from left to right and then top-down.

1 2 3

4 5 6

Figure: Order of gure panels


Tan Do (VGU) Introduction to MATLAB Lecture 4 8 / 25
2D plotting
Example Compare f (x) = sin(x) and g(x) = sin(10x) over [−π, π].
Code
x = -pi:0.01:pi;
f = sin(x);
subplot(1,2,1)
plot(x,f), xlabel(’x’), ylabel(’f’), title(’Plot of f(x) = sin(x)’)

g = sin(10*x);
subplot(1,2,2)
plot(x,g), xlabel(’x’), ylabel(’g’), title(’Plot of g(x) = sin(10x)’)

Output

Tan Do (VGU) Introduction to MATLAB Lecture 4 9 / 25


2D plotting

Saving gures

To save a gure, you can either


click on the save button available on the toolbar in the Figure menu
or
Go to the File menu and click on Save or Save as.

The default extension for a gure in MATLAB is .fig.

You can also save a gure with other extensions too (e.g., .png or .jpg,
etc.) for other purposes.

The saved gure will then be available in the Current Folder window. To
open the gure , double-click on it.

Tan Do (VGU) Introduction to MATLAB Lecture 4 10 / 25


2D plotting

Marker symbols, line types and colors

Data markers Line types Colors


Dot · Solid line - Black k
Asterisk * Dashed line -- Blue b
Cross × Dash-dotted line -. Cyan c
Circle o Dotted line : Green g
Plus sign + Magenta m
Square () s Red r
Diamond () d White w
Five-pointed star (?) p Yellow y

Tan Do (VGU) Introduction to MATLAB Lecture 4 11 / 25


2D plotting

Examples
The position p (in meters) of a projectile is observed at dierent times t (in seconds).
h i
t= 0 1 2 3 4 5 6 7 8

and h i
p = 100 120.41 131.64 133.69 126.56 110.25 84.76 50.09 6.24 .

We want to plot the trajectory of the projectile.


t = 0:8;
p = [100 120.41 131.64 133.69 126.56 110.25 84.76 50.09 6.24];
plot(t,p,’o’), xlabel(’tine (in seconds)’), ylabel(’position (in meters)’)
title(’The trajectory of the projectile’)

This produces a plot with data marker o.

Tan Do (VGU) Introduction to MATLAB Lecture 4 12 / 25


2D plotting
To change the color of the data marker o to red, use
plot(t,p,’ro’)
This produces

To connect data points with line segments, you have to plot the data twice.
plot(t,p,t,p,’ro’)
This produces

Tan Do (VGU) Introduction to MATLAB Lecture 4 13 / 25


2D plotting

Next we illustrate how to use line types in plotting.


Suppose we want to plot y = sin(x) over [0, π] with - - line type.
In MATLAB type
fplot(@sin,[0 pi],’- -’)
This gives

To change the color of the dashed line to green, type


fplot(@sin,[0 pi],’g- -’)
Tan Do (VGU) Introduction to MATLAB Lecture 4 14 / 25
2D plotting

Labeling curves and data


When plotting more than one curves on the same graph, we must either use a legend or put

labels next to the curves to distinguish between them.

The following code produces the plot above.

x = 0:0.01:2*pi;
y = sin(x);
z = cos(x);
plot(x,y,’r’,x,z,’b--’), xlabel(’x’), ylabel(’y’),
legend(’y = sin(x)’,’y = cos(x)’)
Tan Do (VGU) Introduction to MATLAB Lecture 4 15 / 25
2D plotting

You can also put labels next to the curves to distinguish them.

For that you can either use


gtext command: place a label by using the mouse. The syntax is
gtext(’string’), where string is a label text.

text command: require to specify the coordinates of the label. The


syntax is text(x,y,’string’), where string is a label text
and (x, y) its coordinate.
Finding a suitable coordinate will require some trial and error.

Tan Do (VGU) Introduction to MATLAB Lecture 4 16 / 25


2D plotting

hold on command

On page 15 we present a way to plot 2 curves on the same graph.

Another way to do this is to use the hold on command.

Back to the example on page 15, the following code produces the same
graph.

plot(x,y,’r’)
hold on
plot(x,z,’b-’)

Tan Do (VGU) Introduction to MATLAB Lecture 4 17 / 25


3D plotting

3D line plots

Lines in 3D space can be plotted with the plot3 function.


Its syntax is plot3(x,y,z).

Example Suppose we want to plot the 3D curve dened by


x = e−0.05t sin t, y = e−0.05t cos t, z = t,
where t ∈ [0, 10π].
The nal result should look like

Tan Do (VGU) Introduction to MATLAB Lecture 4 18 / 25


3D plotting

Here is the code:

t = 0:pi/50:10*pi;
plot3(exp(-0.05*t).*sin(t), exp(-0.05*t).*cos(t), t)
xlabel(’x’), ylabel(’y’), zlabel(’z’)
grid

Note that the grid and label functions work with plot3.
zlabel is used to label the z-axis.

We can also add title, specify line type and color to enhance the plot:

t = 0:pi/50:10*pi;
plot3(exp(-0.05*t).*sin(t), exp(-0.05*t).*cos(t), t, ’ro’)
xlabel(’x’), ylabel(’y’), zlabel(’z’)
grid
title(’A spiral curve in 3D’)

Tan Do (VGU) Introduction to MATLAB Lecture 4 19 / 25


3D plotting

The result is:

Exercise Plot the helix


x = sin t, y = cos t, z = t3 ,
where 0 ≤ t ≤ 5π.
Tan Do (VGU) Introduction to MATLAB Lecture 4 20 / 25
3D plotting

Surface mesh plot

The function z = f (x, y) represents a surface when plotted on xyz axes.


We use mesh function to plot such a surface.
But before you can use this function, you have to generate a grid of points in the
xy -plane and then evaluate f at these points.

To generate a grid of points, we use the meshgrid function. Its syntax is

x = xmin: xspacing : xmax;


y = ymin : yspacing : ymax;
[X, Y] = meshgrid(x,y)

which gives the coordinates of a rectangle grid with one corner at (xmin, ymin) and
the opposite corner at (xmax, ymax).

Each rectangular panel in the grid will have width xspacing and depth yspacing.

Tan Do (VGU) Introduction to MATLAB Lecture 4 21 / 25


3D plotting

Example

>> x = 1:4;
>> y = 2:2:6;
>> [X, Y] = meshgrid(x,y)
X =
1 2 3 4 (4, 6)
1 2 3 4
1 2 3 4

Y =
(1, 4)

2 2 2 2
4 4 4 4
6 6 6 6

(1, 2) (2, 2) ...

Tan Do (VGU) Introduction to MATLAB Lecture 4 22 / 25


3D plotting
Next we will use mesh to generate the surface z = xe−(x−y over the domain
2 2
) −y 2

−2 ≤ x ≤ 2 and −2 ≤ y ≤ 2.
The code is
x = -2:0.1:2;
y = -2:0.1:2;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-(X-Y.^2).^2 - Y.^2);
mesh(X,Y,Z)
xlabel(’x’), ylabel(’y’), zlabel(’z’)

which gives

Tan Do (VGU) Introduction to MATLAB Lecture 4 23 / 25


3D plotting

Note that in the code above, x and y refers to the same vector.
In this case, we could have made the code more concise using:

[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-(X-Y.^2).^2 - Y.^2);
mesh(X,Y,Z)
xlabel(’x’), ylabel(’y’), zlabel(’z’)

Remember that the spacing should not be too small for x and y values because:
it will make the surface dicult to visualize, and
the vectors X and Y will be too large.

Tan Do (VGU) Introduction to MATLAB Lecture 4 24 / 25


3D plotting
Another way to generate the surface z = xe−(x−y over the domain −2 ≤ x ≤ 2
2 2
) −y 2

and −2 ≤ y ≤ 2 is to use the surf function.

surf is basically the same as mesh.


The only dierence is the surface generated by surf will be shaded.
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-(X-Y.^2).^2 - Y.^2);
surf(X,Y,Z)
xlabel(’x’), ylabel(’y’), zlabel(’z’)

Tan Do (VGU) Introduction to MATLAB Lecture 4 25 / 25

You might also like