Thursday, April 7, 2011

Plotting Data

2D Plots

The following table summarizes the list of functions provided by MATLAB for the creation and customization of two-dimensional plots.
MATLAB Plot Functions
FunctionDescription
plotcreates a plot
loglogcreates a plot with log scales for both axes
semilogxuses a log scale for the x-axis
semilogyuses a log scale for the y-axis
titleadds a title to the plot
xlabeladda a label to the x-axis
ylabeladds a label to the y-axis
textdisplays a text string at a specified location
gtextsame as text but allows use of the mouse to position text
gridturns on grid lines
The plot function can accept one, two, or more arguments and produces a plot of the data contained in the arguments. When a single vector argument is passed to plot, the elements of the vector form the dependent data and the index of the elements form the dependent data. For example the following sequence of commands:


>> x=[1 4 9 16 25 36 49 64 81 100]
>> plot(x)
results in the plot displayed in Figure 1. The output is a very simple graph that lets you painlessly view your data. A specific set of values for the independent axis can be passed to the plot function via additional arguments. It is also possible to make multiple plots on a single graph. For example, if a plot of the functions x=sin(t) and y=cos(t) for values of t ranging from -p to +p, the following sequence of commands could be used:

>> t=-pi:pi/100:pi;
>> x=sin(t);
>> y=cos(t);
>> plot(t,x,'r-',t,y,'g--');
>> title('t vs. sin(t) and cos(t)')
>> xlabel('t')
>> ylabel('sin(t) and cos(t)')
[Missing image] Figure 1. Simple Plot

The resulting plot is shown in Figure 2.
[Missing image]
Figure 2. Multiple Plots

There are a some points of interest in the sequence of commands displayed above:

  • The semi-colons at the end of some of the commands above indicate to MATLAB that the result of the command should not be echoed to the screen.
  • The first command generates a vector of values between the range [-p,p] increments of p/100 and assigns the name t to the vector
  • The plot command makes plots of t versus sin(t) using a solid red line (r-) and t versus cos(t) using a dashed green line (g--).
  • The title, xlabel, and ylabel commands simply specify the title, x-axis label and y-axis labels of the graph.
The arguments to plot enclosed in quotes specify the type and color of the line used to draw the line connecting the datapoints. Table 3 below lists the colors and symbols that are provided by MATLAB:
MATLAB Plot Symbols
SymbolColorSymbolLinestyle
yyellow.point
mmagentaocircle
ccyanxx-mark
rred+plus
ggreen*star
bblue-solid
wwhite:dotted
kblack-.dash-dot
--dashed

The plot function can also accept matrices as arguments. The are four cases that the user should be aware of when passing matrices as arguments to the plot function. The four cases and their ramifications are:


2. A single matrix argument is passed to plot. In this situation, each column of the matrix is treated as a dependent dataset and the row number is treated as the independent dataset.

3. Two arguments with the first argument being a vector, x, and the second argument being a matrix ,Y , are passed to plot. The rows OR columns of Y are plotted versus the vector x. The selection of wether the rows or columns should be used depends on the size of the vector x. If Y is a square matrix, then its columns are used.

4. Same as in case 2 above except that the order of the arguments are reversed. In this case the vector x is plotted versus each row or column of Y.

5. If a matrix, X, and a matrix, Y, are passed as arguments to the plot function, then the columns of Y are plotted versus the columns of X.
The following examples illustrate each of the cases listed above.
Example 1: X is a matrix argument to plot:
[Missing image]

>>X=[1 2 3; 4 5 6; 7 8 9];
>>plot(X);
Figure 3. Example 1







Example 2: x is a vector and is the first argument to[Missing image] plot, and Y is a matrix and is the second argument to plot:


>>x=[2 4 6 8 10];
>>Y=[3 5 7 9 11; 4 8 12 16 20];
>>plot(x,Y,'*');
Figure 4. Example 2
[Missing image]Example 3: x is a vector and is the second argument to plot, and Y is a matrix and is the first argument to plot - the data from Example 2 is used in this example.:


>>plot(Y,x,'o');

Figure 5. Example 3




Example 4: both X and Y are matrices and are arguments to plot.[Missing image]


>>X=[1 2 3; 4 5 6; 7 8 9];
>>Y=[2 4 6; 16 25 36; 10 11 12];
>>plot(X,Y);
Figure 6. Example 4





Plotting Data Contained in a Data File

Frequently, you will need to plot data that has been saved in a file. This data may have been generated by a program or entered by a user by hand. In such an instance, you can use the load command to read in the data from the file.As an example, consider a data file called rungKut.dat that at contains three columns of data with the first column representing the independent dataset and the remaining two columns representing 2 dependent datasets. The first step is to read the data into a matrix. Once the data has been loaded into a matrix, plotting of the data can be performed in the same manner as was discussed in the previous section. The load command automatically assigns the data contained in file to a matrix with the same name as the file. The following example illustrates how the data from rungKat.dat is read into MATLAB and the graphed:


>> load rungKut.dat
>> plot(rungKut(:,1), rungKut(:,2), 'g-', rungKut(:,1), rungKut(:,3), 'ro')
the resulting graph is displayed below.

Note that for data that is imprecisely aligned (i.e. not in the form of a matrix), the fscanf function can be used from within matlab. The fscanf function works in conjunction with the fopen and fclose functions and is used in a manner similar to their C counterparts. For more information, refer to the online help for these functions.
[Missing image]
Figure 7. Plot of Data Contained in a Data File

4.7. 3D Plots

MATLAB provides several functions for the visualization of 3-dimensional data. In Table 3 below, a list of these functions along witha brief description is presented.
MATLAB 3D Plot Functions
FunctionDescription
plot3plots lines and points in 3D
contour, countour3creates contour plots
pcolordraws a rectangular array of cells whose colors are determined by matrix elements
imagedraws a matrix as an image by mapping the elements of the matrix to the current color map
mesh, meshc, meshmcreates 3D perspective plots of matrix elements displayed as heights above an underlying plane.
surf, surfc, surfla combination of the mesh and contour functions
fill3creates a 3D polygon and fills it with solid or interpolated colors.

Lines

The plot3 function works in a similar manner to the plot function. It requires an additional parameter that specifies the Z-coordinates of the points to be plotted. For example, the statements:

>>t = 0:pi/50:10*pi;
>>plot3(1.0./(1.0 + tan(t)), sin(t), t)
produces the following plot:
[Missing image]
Figure 8. Arbitrary 3D Plot

If the arguments to plot3d are matrices of the same size, then the columns of the 3 matrices are used as input data for the line plots.

4.7.2. Surfaces

The mesh function is used to generate surface plots of data. This function is particularly useful in visualizing large matrices. When mesh is invoked with a single matrix arguent, the values contained in the matrix are used as Z-axis data and the indices of the matrix are used as X- and Y-axes data.X- and Y-axes data can be explicitly specified by passing additional parameters to the mesh function. Note that if X- and Y-axes data are to be explicitly specified, then the matrices containing the X and Y data have to preceed the Z data matrix in the call to mesh. The X- and Y-axes data may also be explicitly passed to the mesh function using vectors instead of matrices. In such a case, the vectors are repeated to form a matrix of the correct size - in other words the Z data is plotted over a regularly spaced rectanguler grid. The order in which the vectors are utilized in the plot is:
(x(j), y(i), z(i,j))
The following examples illustrate these concepts.


>> x=-10:.5:10;
>> y=x;
>> [X Y]=meshgrid(x,y);
>> R=sqrt(X.^2+Y.^2) + eps;
>> Z=sin(R)./R;
>> mesh(Z);
The meshgrid function generates 2 matrices from the x and y vectors. The rows of the resulting matrix X are formed by repeating the vector x and the columns of the matrix Y are formed by repeating the vector y. The result of the mesh function is displayed below.
[Missing image]
Figure 9. Sombrero Plot

Note in the preceeding example that the X- and Y-axes range from 0 to 40 and not from -10 to 10 which is the range of the X and Y datasets. The following example illustrates the explicit specification of X- and Y-axes data:


>> x=-5:0.5:5;
>> y=x;
>> [X Y]=meshgrid(x,y);
>> R=sqrt(25.0 - X.^2 - Y.^2);
>> for i = 1:21           
      for j = 1:21           
         if imag(R(i,j)) ~= 0
            R(i,j) = 0+0i;   
         end
      end
   end
>> mesh(X,Y,R);
This example also illustrates the use of MATLAB programming constructs to selectively modify matrix elements. The do loops iterate over all elements of the matrix R and selectively replaces its imaginary elements with the value 0. The result of the call to mesh is displayed below.
[Missing image]
Figure 10. Hemisphere


4.8. Combining Multiple Plots on a Single Page

You can combine multiple plots on the same page by using calls to the subplot(a,b,x) command. This command breaks the figure window into a a-by-b matrix of small subplots and selects the x-th subplot for the current plot. The subplots are numbered starting at 1 and increasing along rows to the value mxn at bottom right of the matrix.

>> [X Y Z]=cylinder(4*cos(t));
>> subplot(2,2,1);
>> mesh(X);
>> subplot(2,2,2);
>> mesh(Y)
>> x=-2*pi:0.1:2*pi;
>> subplot(2,2,3);
>> plot(x,sin(x));
>> subplot(2,2,4);
>> plot(x, log(x);
The result of the sequence of commands above is displayed below.

[Missing image]
Figure 11. Multiple Plots on a Single Chart

4.1. - 2D Plots
4.6. - Plotting Data Contained in a Data File
4.7. - 3D Plots
4.7.1. - Lines
4.7.2. - Surfaces
4.8. - Combining Multiple Plots on a Single Page

No comments:

Post a Comment