Chapter#4
Chapter#4
Two-Dimensional Plots
Plots are a very useful tool for presenting information in science and
engineering, where MATLAB is mostly used. MATLAB has many commands
that can be used for creating different types of plots.
This chapter describes how MATLAB can be used to create and format many
types of two-dimensional plots.
THE plot COMMAND
The plot command is used to create two-dimensional plots. The simplest form of the command is:
The arguments x and y are each a vector (one-dimensional array). The two vectors must have the
same number of elements. When the plot command is executed, a figure is created in the Figure
Window.
Example:
Once the plot command is executed, the Figure Window opens and the plot is
displayed, as shown in Figure
Some examples:
plot(x , y) A blue solid line connects the points with no markers (default).
plot(x, y, ‘r ’) A red solid line connects the points.
plot(x ,y ,‘--y’) A yellow dashed line connects the points.
plot(x ,y ,‘*’) The points are marked with * (no line between the points).
plot(x ,y ,‘ g :d’) A green dotted line connects the points that are marked with diamond
markers.
Property Name and Property Value:
Properties are optional and can be used to specify the thickness of the line, the size of the marker,
and the colors of the marker’s edge line and fill. The Property Name is typed as a string, followed
by a comma and a value for the property, all inside the plot command.
plot(x,y,‘- o’,‘LineWidth’,2,‘markersize’,12,‘MarkerEdgeColor’,‘g’,‘markerfacecolor’,‘y’)
Creates a plot that connects the points with a magenta solid line and circles as markers at the points.
The line width is 2 points and the size of the circle markers is 12 points. The markers have a green
edge line and yellow filling.
Plot of Given Data In this case given data is used to create vectors that are then used in the plot
command. The following table contains sales data of a company from 1988 to 1994.
To plot this data, the list of years is assigned to one vector (named yr), and the corresponding sales data is assigned to a
second vector (named sle). The Command Window where the vectors are created and the plot command is used is shown
below:
Plot of a Function: In many situations there is a need to plot a given function.
This can be done in MATLAB by using the plot or the fplot command.
The fplot command plots a function with the form y = f(x) between specified limits. The command has
the form:
The function should be typed in the form of an anonymous function The form of an anonymous
function is: @ (x) f(x). For example, if the function f(x) = 8x2 + 5cos (x) is to be plotted, it is typed as:
@ (x) 8*x.^2+5*cos(x).
The limits argument is a vector with two elements that specify the domain of x [xmin,xmax], or a
vector with four elements that specifies the domain of x and the limits of the y-axis
[xmin,xmax,ymin,ymax].
The line specifiers are the same as in the plot command.
As an example, the plot command is used to plot the function y =3.5–0.5x cos(6x) for –2 ≤ x ≤ 4. A
program that plots this function is shown in the following script file.
plot(x,y,u,v,t,h)
creates three graphs—y vs. x, v vs. u, and h vs. t—all in the same plot. MATLAB
automatically plots the graphs in different colors so that they can be identified. It is also
possible to add line specifiers following each pair. For example the command
plot(x,y,‘-b’,u,v,‘--r’,t,h,‘g:’)
plots y vs. x with a solid blue line, v vs.u with a dashed red line, and h vs. t with a
dotted green line.
2)Using the hold on and hold off Commands
To plot several graphs using the hold on and hold off commands,
one graph is plotted first with the plot command. Then the hold on
command is typed. This keeps the Figure Window with the first plot open,
Additional graphs can be added with plot commands.
Each plot command creates a graph that is added to that figure.
The hold off command stops this process.
3) Using the line Command
With the line command additional graphs (lines) can be added to a plot that already exists. The
form of the line command is:
line(x,y,‘linestyle’,‘--’,‘color’,‘r’,‘marker’,‘o’)
The major difference between the plot and line commands is that the plot command starts a
new plot every time it is executed, while the line command adds lines to a plot that already
exists.
To make a plot that has several graphs, a plot command is typed first and then line commands
are typed for additional graphs.
Example: Plotting a function and its derivatives: Plot the function y = 3x3 – 26x + 10 , and its first
and second derivatives, for –2 ≤ x ≤ 4, all in the same plot.
Solution
The first derivative of the function is:y' = 9x2 – 26
The second derivative of the function is:y'' = 18x
A script file that creates a vector x and calculates the values of y, y′ , and y″ is:
Using plot method
Using hold on hold off method
title(‘text as string’)
The text command:
text(x,y,‘text as string’)
gtext(‘text as string’)
The text command places the text in the figure such that the first character is positioned at the
point with the coordinates x, y (according to the axes of the figure). The gtext command places
the text at a position specified by the user.
The legend command:
The legend shows a sample of the line type of each graph that is plotted, and places a label, specified by the
user, beside the line sample.
legend(‘string1’,‘string2’, ..... ,‘Location’,‘pos’)
The strings are the labels
The text in the string that is included in the command and is displayed when the command is executed
can be formatted.
The formatting can be used to define the font, size, position (superscript, subscript), style (italic, bold,
etc.), and color of the characters, the color of the background, and many other details of the display.
y is a vector with the data points. MATLAB divides the range of the data points into 10
equally spaced subranges (bins) and then plots the number of data points in each bin.
example, the following data points are the daily maximum temperature (in °F) in Washington, DC, during
the month of April 2002: 58 73 73 53 50 48 56 73 73 66 69 63 74 82 84 91 93 89 91 80 59 69 56 64 63 66
64 74 63 69. A histogram of this data is obtained with the commands:
The number of bins can be defined to be different than 10. This can be done either by specifying the
number of bins, or by specifying the center point of each bin
hist(y,nbins) or hist(y,x)
nbins is a scalar that defines the number of bins.
x is a vector that specifies the location of the center of each bin.
hist(y,3)
POLAR PLOTS
Polar coordinates, in which the position of a point in a plane is defined by the angle and the radius
(distance) to the point, are frequently used in the solution of science and engineering problems. The polar
command is used to plot functions in polar coordinates. The command has the form:
MULTIPLE PLOTS ON THE SAME PAGE
Multiple plots can be created on the same page with the subplot command , which has the form:
x = linspace(0,10);
y1 = sin(x);
y2 = sin(5*x);
subplot(2,1,1);
plot(x,y1)
subplot(2,1,2);
plot(x,y2)
Create a figure divided into four subplots. Plot a sine wave in each one and title each subplot.
subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')
subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')
subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')
subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')
Create a figure containing with three subplots. Create two subplots across the upper half of the figure and a
third subplot that spans the lower half of the figure. Add titles to each subplot..
subplot(2,2,1);
x = linspace(-3.8,3.8);
y1 = cos(x);
plot(x,y1);
title('Subplot 1: Cosine')
subplot(2,2,2);
y2 = 1 - x.^2./2 + x.^4./24;
plot(x,y2,'g');
title('Subplot 2: Polynomial')
subplot(2,2,[3,4]);
plot(x,y1,'b',x,y2,'g');
title('Subplot 3 and 4: Both')