**50%**off this eBook here

### Sage Beginner's Guide — Save 50%

Unlock the full potential of Sage for simplifying and automating mathematical computing with this book and eBook

Sage can make 3D plots for visualizing functions of two variables, as well as parametric plots that create three-dimensional surfaces. It also has a variety of tools for making two dimensional representations of three-dimensional surfaces.

In this article by **Craig Finch**, author of Sage Beginner's Guide, we will make interactive 3D plots of functions of two variables.

Read more about this book |

*(For more resources related to this topic, see here.)*

# Time for action – make an interactive 3D plot

Let's make an interactive 3D plot.

var('x, y')

p3d = plot3d(y^2 + 1 - x^3 - x, (x, -pi, pi), (y, -pi, pi))

p3d.show()

If you run this example in the notebook interface, a Java applet called Jmol will run in the cell below the code. If you run it from the interactive shell, Jmol will launch as a stand-alone application. Clicking and dragging on the figure with the left mouse button will rotate the plot in 3D space. Clicking and dragging with the centre button, or moving the scroll wheel, zooms in and out. Right-clicking brings up a menu that allows you to set various options for Jmol. Since Jmol is also used to visualize the 3D structures of molecules, some of the options are not relevant for plotting functions. Here is a screenshot of the function, plotted with Jmol:

## What just happened?

We made a cool 3D plot that allowed us to explore a function of two variables. When running Jmol as an applet in a worksheet, you can click on the "Get Image" link below the plot to save an image of the plot in its current state. However, the image quality is not particularly high because it is saved in JPEG format. When Jmol is called from the command line, it runs as a stand-alone application, and more options are available. You can save files in JPEG, GIF, PPM, PNG, or PDF format. Note that the PDF format is a bitmap embedded in a PDF file, rather than a true vector representation of the surface. The syntax for using plot3d is very simple:

plot3d(f(x,y), (x, x_min, x_max), (y, y_min, y_max))

There are a few optional arguments to the *show* method that you can use to alter the appearance of the plot. Setting *mesh=True* plots a mesh on the surface, and setting *dots=True* plots a small sphere at each point. You can also use the *transformation* keyword argument to apply a transformation to the data—see the *plot3d* documentation for more information.

# Higher quality output

We can improve the quality of saved images using ray tracing, which is an algorithm for generating images that is based on optical principles. Sage comes with ray tracing software called Tachyon, which can be used to view 3D plots. To activate Tachyon, use the *show* method with the *viewer* keyword as shown below:

p3d.show(viewer='tachyon', frame=False, axes=True)

Depending on the speed of your computer, the ray tracing may require a few seconds to a few minutes.

The *frame* keyword selects whether or not to draw a box around the outer limits of the plot, while the *axes* keyword determines whether or not the axes are drawn.

# Parametric 3D plotting

Sage can also plot functions of two variables that are defined in terms of a parameter. You can make very complex surfaces in this way.

# Time for action – parametric plots in 3D

We will plot two interlocking rings to demonstrate how complex surfaces are easily plotted using three functions of two parameters:

var('u, v')

f1 = (4 + (3 + cos(v)) * sin(u), 4 + (3 + cos(v)) * cos(u),

4 + sin(v))

f2 = (8 + (3 + cos(v)) * cos(u), 3 + sin(v), 4 + (3 + cos(v))

* sin(u))

p1 = parametric_plot3d(f1, (u, 0, 2 * pi), (v, 0, 2 * pi),

texture="red")

p2 = parametric_plot3d(f2, (u, 0, 2 * pi), (v, 0, 2 * pi),

texture="blue")

combination = p1 + p2

combination.show()

The result should look like this:

## What just happened?

We made a very complex 3D shape using the *parametric_plot3d* function. The optional arguments for this function are the same as the options for the *plot3d* function.

# Contour plots

Sage can also make contour plots, which are 2-D representations of 3-D surfaces. While 3D plots are eye-catching, a 2D plot can be a more practical way to convey information about the function or data set.

# Time for action – making some contour plots

The following code will demonstrate four different ways to make a 2D plot of a 3D surface with Sage:

var('x, y')

text_coords = (2, -3.5)

cp = contour_plot(y^2 + 1 - x^3 - x, (x, -3, 3), (y, -3, 3),

contours=8, linewidths=srange(0.5, 4.0, 0.5), fill=False,

labels=True, label_colors='black', cmap='gray', colorbar=False)

cp += text("Contour", text_coords)

ip = implicit_plot(y^2 + 1 - x^3 - x, (x, -3, 3), (y, -3, 3))

ip += text("Implicit", text_coords)

rp = region_plot(y^2 + 1 - x^3 - x < 0, (x, -3, 3), (y, -3, 3),

incol=(0.8, 0.8, 0.8)) # color is an (R,G,B) tuple

rp += text("Region", text_coords)

dp = density_plot(y^2 + 1 - x^3 - x, (x, -3, 3), (y, -3, 3))

dp += text("Density", text_coords)

show(graphics_array([cp, ip, rp, dp], 2, 2), aspect_ratio=1,

figsize=(6, 6))

The output should be as follows:

## What just happened?

The plots we made demonstrate four different ways of visualizing the function we plotted in the previous example. All four functions follow the same syntax as *plot3d*:

contour_plot(f(x,y), (x, x_min, x_max), (y, y_min, y_max))

*contour_plot* plots level curves on the surface. In other words, z is constant on each curve. *implicit_plot* does the same thing, but only plots the curve where z=0. *region_plot* determines the curve for which z=0, and then fills in the region where z<0. Finally, *density_plot* converts the z value of the function to a color value and plots a color map of the z values over the x-y plane. We used the contour plot to demonstrate some of the keyword arguments that can be used to control the appearance of the plot. Here is a summary of the options we used:

Keyword |
Description |

contours | The number of contours to draw |

linewidths | A list of line widths, corresponding to the number of contours |

fill | True to fill in between the contours |

labels | True to label each contour |

label_colors | Color to use for labels |

cmap | Color map to use for contour lines |

colorbar | True to display the a scale bar showing the color map |

# Summary

In this article we learned about making three-dimensional plots and contour plots.

**Further resources on this subject:**

- Plotting Data with Sage [Article]
- Creating Line Graphs in R [Article]
- Graphical Capabilities of R [Article]
- What Can You Do with Sage Math? [Article]
- Python Multimedia: Enhancing Images [Article]
- Python Multimedia: Fun with Animations using Pyglet [Article]

Unlock the full potential of Sage for simplifying and automating mathematical computing with this book and eBook |

## About the Author :

## Craig Finch

Craig Finch is a Ph. D. candidate in the Modeling and Simulation program at the University of Central Florida (UCF). He earned a Bachelor of Science degree from the University of Illinois at Urbana-Champaign and a Master of Science degree from UCF, both in electrical engineering. Craig worked as a design engineer for TriQuint Semiconductor, and currently works as a research assistant in the Hybrid Systems Lab at the UCF NanoScience Technology Center. Craig's professional goal is to develop tools for computational science and engineering and use them to solve difficult problems. In particular, he is interested in developing tools to help biologists study living systems. Craig is committed to using, developing, and promoting open-source software. He provides documentation and "how-to" examples on his blog at http://www.shocksolution.com.

I would like to thank my advisers, Dr. J. Hickman and Dr. Tom Clarke, for giving me the opportunity to pursue my doctorate. I would also like to thank my parents for buying the Apple IIGS computer that started it all.

## Post new comment