Introduction to Matlab

Mathematics University of Queensland Mathematics

5  Plotting Lines and Data

This section shows how to produce simple plots of lines and data.

Suppose we wish to plot some points. For example we are given the following table of experimental results.

xk .5 .7 .9 1.3 1.7 1.8
yk .1 .2 .75 1.5 2.1 2.4

To work with the data in Matlab set up two column vectors x and y.

x = [.5 .7 .9 1.3 1.7 1.8 ]'
y = [.1 .2 .75 1.5 2.1 2.4 ]'

(Vectors are discussed in detail in the next lesson; but we can use them to draw graphs without knowing all the details.) To graph y against x use the plot command.

plot(x,y,'x')

The graph should now appear. (If not, it may be hidden behind other windows. Click on the icon `Figure No. 1' on the Windows task bar to bring the graph to the front.)

This graph marks the points with an `x'. Other types of points can be used by changing the `x' in the plot command. (Use help plot to find out the details.) From the graph it is clear that the data is approximately linear, whereas this is not so obvious just from the numbers. Good graphs quickly show what is going on!

When you have finished looking at the graph, just click on any visible part of the command window, or on the Matlab icon on the task bar. More commands can then be typed in.

Exercise 15

  1. Plot the above data with the points shown as circles.

  2. Plot the above data with the points joined by lines (use help plot).

  3. Plot the points as green stars.

  4. Find out how to add a title to the plot. (Hint: Use the command help plot. At the end of the help output there is a list of related commands. Find out about one of these with another use of the help command.)

5.1  Adding a Line

From the graph it is clear that the points almost lie on a straight line. Perhaps the points are off the line because of experimental errors. A course in statistics will show how to calculate a `line of best fit' for the data. But even without statistics, the line between the points (.5,0) and (2,3) is a good candidate for `a line fitting the data'. Lets add this line to the plot and see how well it approximates the data. We do this by asking Matlab to plot the points (.5,0) and (2,3) joined by a line.

x_vals = [ .5 2]'        %  The X-coords of the endpoints.
y_vals = [ 0 3 ]'        %  The Y-coordinates
plot( x,y,'x', x_vals,y_vals,'-' )
legend('Original Data', 'Line of Best Fit')
title( 'Data Analysis  (Graph by Effigy Mounds)')

Note that x_vals contains the x-coordinates, y_vals contains the y-coordinates, and the two points are joined by a line because the final argument in the plot command is '-'.

5.2  Hints for Good Graphs

There are many opinions on what makes a good graph. From the scientific viewpoint a simple test is to see whether we can recover the original data easily from the graph. A graph is supposed to add something, not remove information. For example if our data runs from 1 to 1.5 it is a bad idea to use an axis running from 0 to 10, as we will not be able to see the differences between the values. We give two more subtle recommendations..

 Graph. Good and Bad Data Plots

5.2.1  Plot data as points

Often people use the very simplest command to plot data and automatically type just

   plot( x, y )          % The simplest plot command

This simple command does not present the data here very well. It is hard to see how many points were in the original data. It is really better to plot just the points, as the lines between points have no significance; they just help us follow the set of measurements if there are several data sets on the one graph. If we insist on joining the points it is important to mark the individual points as well

Exercise 16

Change the above plot command to show the data more clearly by plotting the data points as small circles joined by dotted lines. (Hint: There should be a third argument in the plot command containing the symbols for a line of dots and the marker type circles; and perhaps a color symbol as well!)

5.2.2  Choose a good scale

In the example in this section, it was easy to see the relationship between x and y from the simple plot of x against y. In more complicated situations, it may be necessary to use different scales to show the data more clearly. Consider the following model results

n 3 5 9 17 33 65
sn .257 .0646 .0151 3.96×10-3 9.78×10-4 2.45×10-4
.

A plot of n against sn directly shows no obvious pattern. (Note the dots, `...', in the next example mean the current line continues on the next line. Do not type these dots if you want to put the whole command on the one line.

 Graph. Good Scaling for Plots

n = [ 3 5 9 17 33 65 ]';
s = [ 2.57e-1  6.46e-2  1.51e-2 ...
             3.96e-3 9.78e-4  2.45e-4 ]' ;
 
    plot( n, s, 'x' )          % This is a poor plot!!
 

In fact it is hard to read the values of sn back from the graph. However a plot of log(n) against log(sn) is much clearer. To produce a plot on a log scale we can use either of the commands

 
plot( log10(n), log10(s), 'x')   % Two good plots
loglog( n, s, 'x')               % using log scales!

It reveals there is almost a linear relationship between the logs of these two quantities.

Exercise 17

  1. Add a title to the last plot above and label the X and Y axes.

  2. Using help loglog find a command which will use a log scale only for the sn data. Is this better or worse then the loglog plot?

  3. (A hard exercise.) After allowing for some experimental error, there is a simple relationship between n and sn. Can you find a mathematical formula for this?

  4. What would be the value of sn when n = 129?