Contour plots in Grapher

Grapher is a handy utility in the Mac OS for making 2D and 3D graphs. I’ve found it useful in quickly visualizing math functions and to check whether certain properties hold true (is this function non-negative/convex/have a stationary point at xxx?). For example, here is a plot of z = x \sin y in the default 3D graph setting:

You can click and drag on the figure to inspect it from different angles. (If you don’t do anything, the figure rotates slowly around the z-axis.) You can modify the figure by clicking the “Inspector” icon in the top-right hand corner of the window and playing around with the settings. For example, by selecting the “Height” option for color, it becomes easier to see how the heights in different parts of the graph compare with each other:

While fun to play with, I find it harder to work with 3D plots; contour plots on a 2D surface are often more informative. I learned recently (from here) that this can be done in Grapher! Go to one of the 2D views instead of the 3D views. To visualize the contour plot for z = f(x,y), simply key in f(x,y) in the equation line:

As with the 3D views, you can click on “Inspector” and play around with the settings there. The plot below shows contours for the graph:

I couldn’t figure out how to get the contours drawn in different colors for different levels. One workaround is to add the level sets of interest manually as new equations:

Math expressions in R plots

Did you know that you can include math expressions in your text annotations of figures in R? This webpage has an excellent tutorial on how to do so. In essence, you can put your plot label (with some syntax) in the function expression(). Here’s a little example of that:

par(mar = c(5, 5, 4, 2))
x <- seq(0, 5, length.out = 500)
plot(x, sin(x^2), type = "l", ylab = expression(sin(x^2)),
    main = expression(paste("Plot of ", sin(x^2), " vs. x")))