4.2: Graphical Representation of Data
MATLAB provides a great variety of functions and techniques for graphical display of data. The flexibility and ease of use of MATLAB's plotting tools is one of its key strengths. In MATLAB graphs are shown in a figure window. Several figure windows can be displayed simultaneously, but only one is active. All graphing commands are applied to the active figure. The command
figure(n)
will activate figure number n or create a new figure indexed by .
Tools for plotting
In this section we summarize some of the most commonly used functions for plotting in MATLAB.

plot:
The
plot
function can take a large number of arguments; use the command
help plot
for further information.
plot(y)
plots the values in vector
y
versus their index.
plot(x,y)
plots the values in vector
y
versus
x
.
The plot function produces a piecewise linear graph between its data values. With enough data points it looks continuous.

stem:
The
command
stem(y)
plots the values in vector
y
as stems from the xaxis that terminate with circles on the data values.
stem
is a natural way to plot sequences that represent discrete sequences.
stem(x,y)
plots the data sequence
y
at the values specified in
x
.

xlabel('string')
labels the xaxis with
string
.

ylabel('string')
labels the yaxis with
string
.

title('string')
gives the plot the title
string
.
We illustrate these commands in Example 3.
Example 3
In this example we plot the function for . To do this, we enter the following sequence of commands:

x = 2:0.2:2;

y = x.^ 2;

figure(1);

plot(x,y);

xlabel('x');

ylabel('y=x^2');

title('Simple plot');

figure(2);

stem(x,y);

xlabel('x');

ylabel('y=x^2');

title('Simple stem plot');
This code creates Figures 4 and 5.
Figure 4
Figure 5
Some more commands that can be helpful when working with plots:

hold on <<p>/
pre>
/hold off
Normally hold is off. This means that the plot command replaces the current plot with the new one. To add a new plot to an existing graph usehold on
.
If you want to overwrite the current plot again, usehold off
. 
legend('plot1','plot2',...,'plot N')
The
legend command provides an easy way to identify individual plots when there are more than one per figure. A legend box will be added with strings matched to the plots.

axis([xmin xmax ymin ymax])
Use the axis command to set the axis as you wish. Use axis on/off to toggle the axis on and off respectively.

subplot(m,n,p)
Divides the figure window into m rows and n columns and selects the p'th subplot as the current plot, e.g
subplot(2,1,1)
divides the figure in two and selects the upper part.
subplot(2,1,2)
selects the lower part.

grid on <<p>/
pre>
/grid off
adds or removes a rectangular grid to your plot.
Example 4
This example illustrates
hold
,
legend
,
and
axis
.
The following sequence of commands give the plots in Figure 6.

x = 3:0.1:3; y1 = x.^2; y2 = x.^2;

figure(1);

plot(x,y1);

hold on;

plot(x,y2,'');

hold off;

xlabel('x');

ylabel('y_1=x^2 and y_2=x^2');

legend('y_1=x^2','y_2=x^2');

figure(2);

plot(x,y1);

hold on;

plot(x,y2,'');

hold off;

xlabel('x');

ylabel('y_1=x^2 and y_2=x^2');

legend('y_1=x^2,'y_2=x^2');

axis([1 1  10 10]);
The result is shown in Figure 6.
Figure 6
Example 5
In this example we illustrate subplot and grid with the following sequence of commands:

x = 3:0.2:3; y1 = x.^2; y2 = x.^2;

subplot(2,1,1);

plot(x,y1);

xlabel('x'); ylabel('y_1=x^2');

grid on;

subplot(2,1,2);

plot(x,y2);

xlabel('x');

ylabel('y_2=x^2');
Now, the result is shown in Figure 7.
Figure 7
Printing and exporting graphics
After you have created your figures you may want to print them or export them to graphic files. In the "File" menu use "Print" to print the figure or "Save As" to save your figure to one of the many available graphics formats. Using these options should be sufficient in most cases, but there are also a large number of adjustments available by using "Export setup", "Page Setup" and "Print Setup".
3D Graphics
We now show a brief example of creating plots. Necessary functions here are
meshgrid
and
mesh
.
In Example 6 we see that
meshgrid
produces and vectors suitable for plotting and that mesh plots as a function of both and .
Example 6
Example: Creating our first 3D plot. We execute the following command sequence to produce the plot in Figure 8.

[x,y] = meshgrid(3:.1:3);

z = x.^+y.^2;

mesh(x,y,z);

xlabel('x');

ylabel('y');

zlabel('z=x^2+y^2);
This code gives us the plot in Figure 8.
Image Attributions
Description
Authors:
Categories:
Grades:
Date Created:
Feb 23, 2012Last Modified:
Apr 29, 2014If you would like to associate files with this None, please make a copy first.