I.5 Two - Dimensional Graphics

I.5 Two - Dimensional Graphics

I.5 Two - Dimensional Graphics

I.5.1Linear X-Y Graphs

The common command for plotting two-dimensional data is the plot command. This command plots sets off data arrays on appropriate X-Y (horizontal and vertical) axes, and connects the points using straight lines. Here is an example of how to use the plot command:

Example I.5.1.1:

»x = 0:0.01:2 * pi;

»y = sin(x);

»plot(x, y)

»

This example creates 629 data points over the range 0x 2 to form the horizontal axis of the plot. It also creates another array, y, containing the corresponding sine of the data in array x. Once the plot command is entered, MATLAB will open a graphics window (Figure Window) and graph the data by using straight lines to connect the points. It also adds numerical scales and tick marks to the axes automatically. If a Figure Window already contains a graph, the plot command clears the current Figure Window and plots a new graph.

The Plot Command

plot(horizontal axis, vertical axis)

Array holding the horizontal variable. Array holding the function to be plotted.

I.5.2Multiple Plots on the Same Axes

There are different ways of plotting more than a set of data on the same set of axes:

1) using the hold command, or

2) using the plot command with a multiple pair of arguments

The HoldCommand

The hold command holds the current plot and all properties of the axes so that the subsequent graphing commands add to the existing command.

Example I.5.2.1:

» x = 0:0.01:2 * pi;

» y = sin(x);

» z = cos(x);

» zz = sin(x + pi / 4);

» plot(x, y)

» hold

Current plot held

» plot(x, z);

» plot(x, zz);

»

Once the hold command is entered, MATLAB continues plotting new graphs on the same axes. You can stop holding the current graphs by either entering another hold command or using the hold off command.

To stop MATLAB from holding the current plots, enter

hold off

or

hold (if a hold command has already been entered before)

Another way of plotting multiple functions on the same set of axes is by giving the plot command to more than a pair of arguments.

Example I.5.2.2:

» hold off

» x = 0:0.01:2 * pi;

» y = sin(x);

» z = cos(x);

» plot(x, y, x, z)

»

Remember

Many functions may be plotted at one time if you supply additional pairs of arguments to the plot command.

If one of the arguments in the plot command is a matrix and the other is a vector, then MATLAB graphs each row of the matrix versus the vector.

Example I.5.2.3:

» x = 0:0.01:2 * pi;

» y = sin(x);

» z = cos(x);

» zz = sin(x + pi / 4);

» mat_y_z_zz = [y; z; zz];

» plot(x, mat_y_z_zz)

»

In this example, you created a matrix, mat_y_z_zz, whose rows were sin(x), cos(x), and sin(x + pi / 4). The plot command graphs the rows of this matrix versus the variable x simultaneously on the same set of axes.

I.5.3Interpretation of Plot Command with a Single Argument

When the plot command is used with a single argument, plot(y), it responds based on the data contained in y. If y is a complex-valued function, then plot(y) is interpreted as plot (Real(y), Imag(y)). In all other instances, the imaginary component of y is ignored.

Example I.5.3.1:

» x = 0:0.01:2 * pi;

» y = sin(x) + cos(x) * i;

» plot(y)

»

In this example, you created a complex array y with Real(y) = sin(x) and

Imag(y) = cos(x). The plot command, plot(y), graphs cos(x) versus sin(x).

When y is a real-valued function, then plot(y) is interpreted as plot(x,y). If there are no arrays involved in the calculation of y, then plot(y) is interpreted as plot(1:length(y),y). That is, y is plotted versus an index of its values. For example, when the single argument is a matrix, then these interpretations are applied to the columns of the matrix.

Example I.5.3.2:

» x = 0:0.01:2 * pi;

» mat_y_z = [sin(x); cos(x)];

» mat_y_z = mat_y_z';

» plot(mat_y_z)

»

In this example, you created a matrix, mat_y_z with its first and second columns being sin(x) and cos(x), respectively. Note that MATLAB interpreted the plot as plotting sin(x) and cos(x) versus its index ( number of rows) not the values of x.

Teaser I.5.3.1:

In the previous plots, you saw different scales for the x-axis.

One of them, divided the x-axis between 0 and 7, the other between 0 and 700. Can you explain why ?

If you could not explain this, see our explanation at the end of this chapter.

I.5.4Line Styles, Markers, and Colors

Various line types, plot symbols and colors may be obtained with plot(x,y,s), where s is a character string made of one or more elements from the following columns labeled “Symbol.”

SymbolColorSymbolMarkerSymbolLine Style

yyellow . point - solid

m magenta o circle : dotted

c cyan x x-mark -. dashdot

r red + plus -- dashed

g green * star

b blue s square

w white d diamond

k black v triangle (down)

^ triangle (up)

< triangle (left)

> triangle (right)

p pentagram

h hexagram

Example I.5.4.1:

» x = 0:0.1:2 * pi;

» y = sin(x);

» z = cos(x);

» plot(x, y, 'kp-', x, z, 'gd--')

»

In this example, y is plotted with black color (k), pentagram marker ( p), and solid lines. The function z is plotted with green color (g), diamond marker (d), and dashed lines (--).

Remember

Line style, marker, and color must follow each pair of the arguments in the plot command. These parameters must be enclosed in single quotation marks, ‘ ’.

plot(x, y, ‘bd*’)

x = horizontal axis

y = vertical axis

b = blue color

d = diamond marker

* = start line style

I.5.5Plot Appearance

MATLAB allows flexibility as to how you want your plot to appear on the screen. You can add grids, label the axes, insert a title, create a legend, and eliminate the box that surrounds your graph.

Adding Grids:

Example I.5.5.1:

» x = 0:0.01:2 * pi;

» plot(y)

» grid on

»

To remove the grid, simply enter

» grid off

»

Labeling Axes

xlabel

Labeling axes is simple!

xlabel(‘whatever you want for x-axis’)

ylabel(‘whatever you want for y-axis’)

» xlabel('This is where the x-label goes.')

» ylabel('This is where the y-label goes.')

»

Titling Your Plot

The title command adds a line of text at the top of the plot.

title(‘This is where the title goes’)

» title('This is where the title goes.')

»

Remember

You can add a label (or any other text string) to any specific location on your plot with the text command.

text ( x,y, ‘text string’)

The point (x,y) represents the coordinates of the center left edge of the text string in units taken from the plot axes.

For example, you can add y = sin(x) at the location (300,0.4) as:

» text(300,0.4,'y = sin(x)')

»

Legends:

Rather than using an individual text string to identify each graph, you may use a legend. The legend command creates a legend box in the upper right corner of the plot and adds any text that you supply to each line in your plot. If you wish to move the legend to a different location, simply click and drag it to the desired location.

Example I.5.5.2:

» x = 0:0.01:2 * pi;

» y = sin(x);

» z = cos(x);

» plot(x, y, '-', x, z, '--')

» legend('- sin(x)', '-- cos(x)')

»

Remember

You can click and drag the legend box to any location which you desire.

To remove the legend, simply enter:

legend off

Remember

To remove the title, x-label, and y-label, enter:

xlabel(‘ ‘)

ylabel(‘ ‘)

title(‘ ‘)

The Axes Box

When you plot a function, the box is “ON.”

To remove the box, simply enter:

box off

To add the box, simply enter:

box on

Scaling Axes

MATLAB automatically scales the axes to fit your data set. To manually scale the axes, you can use the axis command. This gives you complete control over the scaling and appearance of the horizontal and vertical axes of your plot.

Scaling Axes

You can control the scales of the x and y axes by

axis([xmin, xmax, ymin, ymax])

which sets the minimum and maximum values for the horizontal and vertical axes.

Auto Scaling

The default scaling is automatic. However, if you have scaled the axes manually, you can always go back to auto scaling by entering

axis auto

Axes “Off” and Axes “On”

You can turn the axes on or off. The default is on. To turn the axes off, enter

axis off

The axis command is a powerful way of controlling the appearance of your plot. In addition to the above features, you can do much more. To get a complete list of what you can do with the axis command, type “help axis” at the prompt in the Command Window

I.5.6Manipulating Plots

At this time, you should know how to plot more than one set of data on the same set of axes by using the hold command. However, there are times when you would like to plot more than one set of data on different sets of axes in the same window. This can be done by using the subplot command. The subplot command divides the Figure Window into different areas defined by the argument of the subplot. For example:

subplot(m,n,p)

divides the Figure Window into mn rectangular panes (m rows and n columns) and creates a set of axes in the pth pane where the data will be plotted.

Example I.5.6.1:

» clear

» x = 0:0.01:2 * pi;

» y = sin(x);

» z = cos(x);

» zz = sin(x + pi / 4);

» subplot(2, 3, 1)

» plot(y)

» subplot(2, 3, 3)

» plot(z)

» subplot(2, 3, 5)

» plot(zz)

»

Plots of y, z, and zz will be in panes 1, 3, and 5, respectively.

I am stuck in subplot, get me out!

Okay, just close the Figure Window by clicking the left mouse button on the top right corner (X). After the Figure Window has closed, open it by entering the command figure(1) in the Command Window. MATLAB will open a Figure Window that contains no plots. I hope you learned your lesson and learned how to get out of trouble!

ZoomingonPlots

The zoom command,

zoom on

is used to activate the interactive “zoom in” function of the MATLAB. Once this command is entered, clicking the left mouse button on the plot will “zoom in” and clicking the right mouse will “zoom out” the plot.

Filling the Area

You can fill in the area under the curve by using the area command.

» x=0:0.1:2*pi;

» y=sin(x);

» area(x,y)

»

Plotting on log-log and semi-log Scales

The loglog command is the same as the plot command except that the logarithmic scales are used for both axes.

» x=1:2:1000;

» y=x.*x+2*x+300;

» loglog(x,y)

»

The semilogx command is the same as the plot command, except that the horizontal axis is a logarithmic scale.

semilogx(x,y)

This command is the same as plot(x,y)

except that

the x-axis will be on a semi-log and the y-axis on a linear scale.

The semilogy command is the same as the plot command, except that the vertical axis is a logarithmic scale.

semilogy(x,y)

This command is the same as plot(x,y)

except that

the x-axis will be on a linear scale and the y-axis on a semi-log.

Pie Chart

pie(x)

Plots a pie chart of vector x.

pie(x,b)

b is a logical vector describing a slice or slices to be pulled out of the pie chart.

» x = [1 2 2 3 2];

» pie(x)

» subplot(2, 2, 1)

» pie(x)

» subplot(2, 2, 2)

» pie(x, x==min(x))

Bar Graphs

bar(x,y)

This command is the same as plot(x,y)

except that

it plots a bar graph.

Printing Your Results

There are different ways of printing plots. The two most common ways are:

a) Cilck on the Figure Window, choose print from the File menu, or

b) Click on the Figure Window, then type print at the prompt on the Command Window.

In the latter, you can control the print orientation and size, by using the following commands:

» orient landscape

This command tells the printer to change the printout orientation from portrait to landscape.

»orient tall

This command tells the printer to stretch the plot to fill the vertical page.

Teasers’ Answer

Teaser I.5.3.1

The argument of the plot command tells us how the x-axis is scaled. If a single argument is used for the argument of the plot command, i.e., plot(y), then MATLAB uses the minimum and the maximum values of x to scale the x-axis. If a pair of variables is used for the argument of the plot command, i.e., plot(x,y), then MATLAB uses the number of points in the x vector to scale the x-axis.