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.
- 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
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:
- 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.
- 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.
- Increase the resolution – I hate the jaggies.
- Add contours. Play with the slider until the spacing looks nice.
- Decrease opacity to 75% (or whatever you prefer) to show axes and tick marks.
… 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
where and are the x and y components of the vector at any given point (x,y), we can represent this in Grapher as follows:
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
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
Dipoles are even prettier. The pertinent equations for a ±1C dipole located d apart are:
B. The gradient operator
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
Overlaying a vector plot of 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:
you can plot its direction field in Grapher by entering the following:
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
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).