next up previous contents
Next: 2.2 Three-dimensional plots Up: 2.1.3 Other 2-D plots Previous: 2.1.3.6 Function plots   Contents

2.1.3.7 Contour plots

Contour plots are perhaps the easiest way to visualize two-dimensional data. When the function we wish to plot depends on two independent variables, like $f(x,y)$, we need a third dimension in which to show the function values--simple 2-D plots don't work. Consider the function of two Gaussians defined in the $x$-$y$ plane,

\begin{displaymath}f(x,y) = \frac{1}{e^{\vert\vec r -\hat x/2\vert^2}}-\frac{1}{e^{\vert\vec r +\hat x/2\vert^2}}. \end{displaymath}

This function defines an exponential ``hill'' centered at $(\frac{1}{2},0)$ and an exponential ``valley'' centered at $(-\frac{1}{2},0)$. It's not too hard to visualize what an ``elevation map'' of this function would look like. Sketch your guess and then try the following.
% Plotting 2D data with contour
close all; clear all;
points = linspace(-2,2,40); % 40 equally spaced points between
[X Y] = meshgrid(points,points); % Create two arrays, x & y
Z = 1./exp((X-.5).^2+Y.^2)-1./exp((X+.5).^2+Y.^2);
contour(X,Y,Z,30) % The fourth argument says "draw 30 contours"
The command meshgrid is new. It fills the two arrays $X$ and $Y$ with the $x$ and $y$ coordinates, respectively, for each point in the plot (that is, each is a $40\times40$ array of 1600 numbers). Also new is the ./ operator. It means to individually take the inverse of each element of an array (i.e., don't take the inverse of the array as a whole as if it is a matrix).
next up previous contents
Next: 2.2 Three-dimensional plots Up: 2.1.3 Other 2-D plots Previous: 2.1.3.6 Function plots   Contents
Gus Hart 2005-01-28