Notes 03: Plotting Data

Reading: Gilat Ch. 5

Code from class: plotting.m



First examples

Here is an example from MathWork's MATLAB documentaiton
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y);
% Now label the axes and add a title. The characters \pi create the
% symbol π. See "text strings" in the MATLAB Reference documentation for
% more symbols:
xlabel('x = 0:2\pi');
ylabel('Sine of x');
title('Plot of the Sine Function','FontSize',12);
example1

Here's another example from that tutorial.

t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X)
subplot(2,2,2); mesh(Y)
subplot(2,2,3); mesh(Z)
subplot(2,2,4); mesh(X,Y,Z)
example2

Saving a figure to a file


Detailed examples

The plot command

Controlling the axes

By default, MATLAB automatically scales both axes to make good use of the figure window while ensure not to clip any of the plotted data. For example, in our earlier graph of the equation y = x .^ 2, the x-axis goes from 0 to 1.5 because that was the range of values in our vector 0:0.1:1.5 while the y-axis ranges from 0 to 2.5. Yet in the graph where we simultaneously graphed x^2, x^3, and x^4, the y-range was scaled from 0 to 6 so that all three plots fit.

While auto-mode for axes is usually convenient, there are times when we would like to better control the choice. For example, in the graph of x^2, the scale for the x-axis is not the same as the scale for the y-axis. This distorts certain aspects of the graph, for example if trying to estimate the slope of the curve when x=1. We can force the two axes to be drawn with equal scale by using the command axis equal after the initial plot.

x = 0:0.1:1.5;       % steps from 0 to 1.5, at 0.1 intervals
y = x .^ 2;          % y = x^2 parabola
plot(x,y);
axis equal;

We can also set the axes range manually, using a syntax of the form axes([xmin xmax ymin ymax]).

x = 0:0.1:1.5;       % steps from 0 to 1.5, at 0.1 intervals
y = x .^ 2;          % y = x^2 parabola
plot(x,y);
axis([-1 1 -0.5 1.5]);

A separately controllable aspect of the figure is whether grid lines are drawn across the primary pane of the graph. By default, the grid lines are not there, but they can be turned on by giving the command grid on, as in the following.

x = 0:0.1:1.5;                     % steps from 0 to 1.5, at 0.1 intervals
y = [x .^ 2; x .^ 3; x .^ 4];      % three functions in one
plot(x,y);
grid on;

Adding labels

It is possible to augment a figure with a variety of labels

Line styles

We have seen that by default, a graph uses a solid blue line as the primary line style. The line style can be changed by providing what is known as a line specifier as extra arguments to a plot call. The line specifier can be used to control three aspects:

Please see MATLAB documentation for more details. Here is an example.
plot(1 ./ 2 .^ (1:10), 'k*:');     % blac(k), asterisk markers, dotted lines
title('Inverse powers of two');

Note that if you use a line specifier and provide a marker type but not a line type, then the markers will be drawn without the connecting lines.


Combining multiple graphs

Typically, each call to plot causes a new figure to be drawn. However, there are several techniques to combine multiple graphs in a single figure.


Additional 2D graph styles


3D graph styles