## Mac OS X Grapher: Contour and Vector Plots

Whew! This post just kept getting longer and longer, until finally I decided I needed to bite it off if it was ever going to get posted. Today we will discuss contour and vector plots, together with a few simple examples. As always, click on any graph to enlarge.

## I. Contour Plots

### A.  Creating a contour plot

A contour plot is a way to represent a three-dimensional surface on a two-dimensional graph. For example, the function f(x,y) = sin(x)*sin(y) gives the following 3-D surface:This function shows a regular pattern of peaks and valleys and looks a lot like an egg crate.

Say, for whatever reason, we want to represent this surface on a two-dimensional graph. We can’t show the peaks and valleys physically on a flat piece of graph paper, but what if we copied the colors: red to show peaks, blue to show valleys, and green to show ground level.  The result would be a two-dimensional plot that captures the height variation of the original function.

In Grapher we can do this by typing in the equation “sinx*siny” (note there is no equals sign) into the Equation Editor:The result is a regular pattern of reds and blues denoting the same peaks and valleys that we saw in the 3-D plot. It’s like we’re looking down on the egg crate from directly overhead.

### B. Editing contour plots

We can customize our plot with the Inspector:

Note the large curved black arrow in the right lower quadrant of the Inspector. Some modifications require you to “refresh” the plot by clicking the black arrow before they will take effect.  So if you make a change, and nothing happens, start clicking the arrow before you start swearing at Grapher.

• Line – changes the color and thickness of contour lines, if they are displayed (the “Contour” box is checked).
• Opacity slider – controls the opacity of the contour map colors.
• Colors – if unchecked, colors are not displayed.
• “Rainbow” box – allows you to select the color scheme. Default = rainbow, with blue representing lows and red representing peaks. Double-clicking the rainbow brings up a selection of predefined color schemes, with the option to define your own. Here, for example, is our egg crate in moody blues:
• Range – specifies how function values are mapped to colors. For example, our egg crate function oscillates between [-1, 1]. Grapher therefore automatically maps -1 to dark blue and +1 light blue. If for some reason you don’t like Grapher’s choice, you can click the “Custom” box and set the range manually.
• Resolution– changes the fineness of the “grid” over which colors are calculated. Low resolutions result in pixelation, but plot faster. High resolutions give smoother color gradations, but take forever to draw (you will become intimately familiar with the message “Creating Scalar Field…”). I generally keep mine a couple notches south of the maximum.

Lowest Resolution

Highest Resolution

• Contours box – plots contour lines if checked. Contour lines are lines connecting points that have the same “height” (z value) on your function. They function exactly like contour lines on a topographic map. You can control spacing of contour lines with the spacing slider; if necessary you can check the “Custom box” and set your own.

### C. Tweaking contour plots

While creating and editing contour graphs is easy, getting them to look good may be hard. For example, the voltage (electric potential) around a +1C monopole is given by the equation

$\phi = \frac{{kq}} {{\left( {x^2 + y^2 } \right)^{.5} }}$

where q = +1C and k = 1/4πε0 = 8.99 x 10^9 N-m^2/C^2.  If we enter this equation into Grapher, we get this:Not very exciting.  So what went wrong?  Is the plot incorrect?  Nope.  It’s just the age-old problem of the computer taking things too literally.

In three dimensions, a plot of the potential would look like this:The potential goes from 0 (far away from the charge), to infinity (at the charge). Grapher takes a look at that huge range and says, “OK, I’ll color any voltage over 20 bazillion red, and everything else blue.”  So we get a red speck inside a turquoise dot, in the middle of a blue ocean.

We can fix this with some tweaking:

1. Set k=1. This doesn’t change the appearance of our plot, but there’s no sense making Grapher plot numbers on the order of 10^9 if we don’t have to.
2. Change the range. We know the minimum value is zero, and some experimentation shows that a good maximum value is around 3V. This means that any voltage greater than 3V will be colored red in our plot.
3. Increase the resolution – I hate the jaggies.
4. Add contours.  Play with the slider until the spacing looks nice.
5. Decrease opacity to 75% (or whatever you prefer) to show axes and tick marks.

Voilà…

… a pretty picture that nicely depicts the rapid increase in potential as you get get closer to the charge, with a cool little Moiré effect thrown in for free.

## II. Vector Plots

### A.  Creating vector plots

Grapher has the wonderful (and unusual) ability to make nice-looking vector plots. This can be invaluable if you want to visualize a vector field (e.g., electric fields, magnetic fields, or velocity fields).  If we have a vector function of the form

${\mathbf{f}}\left( {x,y} \right) = f_x \left( {x,y} \right){\mathbf{\hat i}} + f_y \left( {x,y} \right){\mathbf{\hat j}}$

where $f_x$ and $f_y$ are the x and y components of the vector at any given point (x,y), we can represent this in Grapher as follows:

$\Delta \left[ {\begin{array}{*{20}c} x \\ y \\ \end{array} } \right] = \left[ {\begin{array}{*{20}c} {f_x \left( {x,y} \right)} \\ {f_y \left( {x,y} \right)} \\ \end{array} } \right]$

where the “∆” symbol and the matrix notation can be entered via the Equation Palette.

For example, let’s look at the electric field (E field) corresponding to our monopole, which is given by the vector equation

${\mathbf{E}} = \frac{{kqx}} {{\left( {x^2 + y^2 } \right)^{1.5} }}{\mathbf{\hat i}} + \frac{{kqy}} {{\left( {x^2 + y^2 } \right)^{1.5} }}{\mathbf{\hat j}}$

Entering this into Grapher gives you this:

Note I scaled k=1 again as it does not affect the appearance of the plot.

### B. Modifying vector plots

We can drag out our trusty Inspector and tweak our plot:

• Line – determines color and thickness of the arrows whenever “Use Colors” is not checked.
• Arrow – allows you to change arrowhead styles.
• Density – changes the number of arrows per unit area. Unfortunately you cannot constrain arrows to appear at particular points; if you must have an arrow at a specific location you have to play with the density slider.
• Vector Size – determines the length of the arrows. If the “Proportional” box is checked, the length of the arrow will be proportional to the actual magnitude of the vector.
• Use Color – allows color mapping of arrows according to vector magnitude. As with contour plots, you can select from several predefined color schemes or create your own.
• Large curled black arrow – refreshes plot after making changes in the Inspector.

As with contour plots, vector plots may need significant tweaking in order to look their best.

Grapher Quirk: I call it a quirk out of charity; it’s really a bug, and an annoying one at that.  When opening a previously saved vector plot file, Grapher sometimes loses the y components of the vectors, leaving you with a bunch of (erroneous) horizontal arrows:

The only fix I have found is to force Grapher to recalculate the vectors.  Make some small change to your equation (for example, change k=1 to k=2), and hit enter. Grapher will recalculate all the vectors and redraw all the arrows, complete with y components.  Only now your formula is wrong.  So now you have to change your formula back to its original form, and press enter again.  Note that you will lose all your custom Inspector modifications in the process (hence, “annoying” and “bug”).

## III. Some More Examples

### A.  Combined electric potential and E field plots

I frequently use contour and vector plots together. For example, we can show the E field and the electric potential of our monopole simultaneously:

Dipoles are even prettier. The pertinent equations for a ±1C dipole located d apart are:

$\phi = kq\left( {\frac{1} {{\left( {x^2 + y^2 } \right)^{.5} }} - \frac{1} {{\left( {x^2 + \left( {y + d} \right)^2 } \right)^{.5} }}} \right)$

${\mathbf{E}} = \left[ {kq\left( {\frac{x} {{\left( {x^2 + y^2 } \right)^{1.5} }} - \frac{x} {{\left( {x^2 + \left( {y + d} \right)^2 } \right)^{1.5} }}} \right)} \right]{\mathbf{\hat i}} + \left[ {kq\left( {\frac{y} {{\left( {x^2 + y^2 } \right)^{1.5} }} - \frac{{y + d}} {{\left( {x^2 + \left( {y + d} \right)^2 } \right)^{1.5} }}} \right)} \right]{\mathbf{\hat j}}$

which give:

Combination contour/vector fields can also be used to illustrate the gradient operator. Going back to our eggcrate function f(x,y) = sin(x)*sin(y), the gradient of this function is a vector field given by

$\nabla f\left( {x,y} \right) = \left( {\cos x\cdot\sin y} \right){\mathbf{\hat i}} + \left( {\sin x\cdot\cos y} \right){\mathbf{\hat j}}$

Overlaying a vector plot of $\nabla f\left( {x,y} \right)$ onto a contour plot of f(x,y) gives:Here I’ve used proportional vectors in order to demonstrate the gradient magnitude. Plots like this can demonstrate various properties of the gradient, e.g.,

• vanishes @ local maxima/minima
• always points uphill
• always perpendicular to contour lines
• has the greatest magnitude whenever contour lines are the closest together

### C. Direction fields (a.k.a. slope fields)

Direction fields are a handy way to visualize the solutions of first-order differential equations without actually having to solve the equation.  If you have a first-order differential equation of the form:

$y' = f(x,y)$

you can plot its direction field in Grapher by entering the following:

$\Delta \left[ {\begin{array}{*{20}c} x \\ y \\ \end{array} } \right] = \left[ {\begin{array}{*{20}c} 1 \\ {f(x,y)} \\ \end{array} } \right]$

The reasoning is as follows.  Direction fields involve plotting the slope of the solution function y(x) at selected (x,y) coordinates. For example, the direction field of the equation $y' = x^2 y - 1$ looks like this:

The slope of the solution function at each point can be calculated from whatever expression is given for y’.  That slope can also be written as dy/dx: the change in y divided by the change in x.

Now, as fate would have it, Grapher thinks of vectors as involving a change in y and a change in x (hence the “∆” nomenclature Grapher uses). So this gives us the idea that we can plot the slope field as a vector field, with the y component of the vector = dy and the x component = dx.  If we do that, each little vector arrow will have a slope = dy/dx, which is what we want.

The bad news is that we do not have independent values for dy and dx; the good news is that we do have the ratio: dy/dx = f(x,y).  And the ratio is all we care about, because that’s what determines the slope.  Therefore we can arbitrarily set dx=1, and let dy = f(x,y).  The slope of the vector is the same, regardless if we write

$\Delta \left[ {\begin{array}{*{20}c} x \\ y \\ \end{array} } \right] = \left[ {\begin{array}{*{20}c} 1 \\ {dy/dx} \\ \end{array} } \right]$

or $\Delta \left[ {\begin{array}{*{20}c} x \\ y \\ \end{array} } \right] = \left[ {\begin{array}{*{20}c} {dx} \\ {dy} \\ \end{array} } \right]$

I plan to discuss slope fields a little more when I get to differential equations.  However, I need to work myself up to that topic :).  The next installment of Grapher: the Unofficial Guide will cover using Grapher for basic calculus (differentiation and integration).

This entry was posted in Grapher and tagged , , , , . Bookmark the permalink.

### 8 Responses to Mac OS X Grapher: Contour and Vector Plots

1. Jason says:

Can you create a solid of revolution? ex) y = sqrt(x), from 0 to 1, rotated about the x-axis?

2. Sk1nk says:

Would love to see a tutorial on slope fields, got huge problems with drawing it for
a) y’=-y
b) y’=-x/y
and can’t wait for someone to show me how to do that

3. Mark says:

I’ve been messing with potentials and vector fields, plotting this:
1/r^2 as a vector field.

Trouble is, I need arrows to be proportional, a problem around r=0! Can I say ‘don’t draw arrows if r<constant

(I actually have the field as an x, y eqn)

4. brotherStefan says:

My question is similar to Jason’s…
I’ve written a 4-page explanation on calculating the volume generated by sweeping a bounded region about an arbitrarily chosen axis of revolution that I can hand out to interested students. Grapher lets me show the plane region to be revolved easily enough, and I can add drama with an overlay of a single representative washer or shell in Pages, but I’d like to be able to depict the resulting solid without having to resort to a 3-D CAD application for the image.

5. Action Bronson says:

Love this post!
One quick question though. I’m plotting this function: z = -2(cos^2x + cos^2y) along with its gradient: 4(cosx)(sinx)*i + 4(cosy)(siny)*j as shown in this post (where i is the unit vector on the x-axis: (1,0) and j is the same for the y-axis: (0,1)). If you plot these, you’ll see that, while correct, the vectors are miss-matched and are not symmetrical around the peaks and valleys. I know this is because the points that grapher chooses to plot happen not to fall in this way, but I was wondering if there’s anyway to realign it and make it prettier.
(If my description is unclear you’ll see what I mean when you plot it yourself)
thanks a lot

6. David says:

Strange Things…

Plotting a vector of the form Delta{x,y}={… , …} where the items inside the matrices are stacked per the example, or even any vector that isn’t Δx=… or Δy=…, results in errors and no vector plot in grapher for me. Grapher v. 2.5 (46)

Errors:
No variable with range for series definition
No variable with range found for differential equation
Invalid custom definition

7. I just came across these posts and really appreciate the time you spent documenting Grapher (a wonderful program that is horribly documented). I was wondering if there is a way to plot an individual vector (not a vector field). For example, suppose you want to plot the vector [3, 2] originating from [1, 4] and terminating at [4, 6]? (Ultimately what I’d like is to mark up a parametrized path with the tangent vector at specific points.)