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

Chapter#4

The document describes how to create and format two-dimensional plots in MATLAB. It discusses the basic plot command and how to specify line styles, colors, and markers. It also covers plotting multiple graphs in the same figure using plot, hold on/off, and line commands. The document provides examples of formatting plots with titles, labels, legends, and adjusting axes. Finally, it briefly mentions other plot types like histograms and bar plots that can effectively present different types of data.

Uploaded by

kawibep229
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Chapter#4

The document describes how to create and format two-dimensional plots in MATLAB. It discusses the basic plot command and how to specify line styles, colors, and markers. It also covers plotting multiple graphs in the same figure using plot, hold on/off, and line commands. The document provides examples of formatting plots with titles, labels, legends, and adjusting axes. Finally, it briefly mentions other plot types like histograms and bar plots that can effectively present different types of data.

Uploaded by

kawibep229
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Plotting and Sub plotting

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

The default line color is blue


The General Form Of Plot Command
 The plot command has additional, optional arguments that can be
used to specify the color and style of the line and the color and type
of markers, if any are desired. With these options the command has
the form:
Line specifiers are optional and can be used to define the style and color of the
line and the type of markers (if markers are desired). The line style specifiers are:

 The line color specifies are:


The marker type specifies are:

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.

 When using fplot command:

fplot(@(x) 3.5.^(-0.5*x).*cos(6*x),[-2 4])


PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT
 There are three methods to plot multiple graphs in one figure. One is by using the plot
command, the second is by using the hold on and hold off commands, and the third is by using
the line command.
1) Using the plot Command
 Two or more graphs can be created in the same plot by typing pairs of vectors inside the plot
command. The command

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

Using Line method


The plot that is created is shown below: A plot of the function y
= 3x3 – 26x + 10 and its first and second derivatives.
FORMATTING A PLOT
Formatting a Plot Using Commands
 The formatting commands are entered after the plot or the fplot command
The xlabel and ylabel commands:
xlabel(‘text as string’)
ylabel(‘text as string’)
The title command:

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

 NE Places the legend at the upper-right corner of the plot (default).


 NW Places the legend at the upper-left corner of the plot.
 SE Places the legend at the lower-right corner of the plot.
 SW Places the legend at the lower-left corner of the plot.
 B Places the legend inside the plot in a location that interferes the least
with the graphs.
 BO Places the legend in a least unused space outside the plot.
Example of a formatted two-dimensional plot.
Formatting the text within the xlabel, ylabel, title, text and legend commands:

 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.

Subscript and superscript:


Several consecutive characters can be displayed as a subscript or a superscript by typing the
characters inside braces { } following the _ or the ^.
 Greek characters:
 Greek characters can be included in the text by typing \name of the letter within the string.
The axis command:
 The axis command can be used to change the range and the appearance of the axes.
 axis([xmin,xmax,ymin,ymax]) Sets the limits of both the x and y axes (xmin,
xmax, ymin, and ymax are numbers).
 axis equal Sets the same scale for both axes.
 axis square Sets the axes region to be square.
 axis tight Sets the axis limits to the range of the data.
The grid command:
 grid on Adds grid lines to the plot.
 grid off Removes grid lines from the plot.
Example of a formatted two-dimensional plot.
PLOTS WITH SPECIAL GRAPHICS
 All the plots that have been presented so far in this chapter are line plots in which the data points are
connected by lines. In many situations plots with different graphics or geometry can present data more
effectively
 These include bar, stairs, stem,and pie plots and many more.
HISTOGRAMS
 Histograms are plots that show the distribution of data. The overall range of a given set of
data points is divided into subranges (bins), and the histogram shows how many data points
are in each bin. The histogram is a vertical bar plot in which the width of each bar is equal to
the range of the corresponding bin and the height of the bar corresponds to the number of
data points in the bin.
 The simplest form of the command is:

 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:

 The command divides the Figure Window into m × n rectangular subplots.


The subplots are arranged like elements in an m × n matrix where each element is a subplot. The subplots
are numbered from 1 through m ⋅ n . The upper left subplot is numbered 1 and the lower right subplot is
numbered m ⋅ n .
Create a figure with two stacked subplots. Plot a sine wave in each one.

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')

You might also like