Contour Plots

Description

Here we will show how to make contour plots in sage for functions with two variables, such as

(1)
\begin{equation} f(x, y) = x^2 + y^2. \end{equation}

Sage Cell

Code

y = var('y')
contour_plot(x^2 + y^2, (x, -10, 10), (y, -10, 10))

Options

Multicolor plots

By using the cmap option, we can make a contour plot in multiple different colors instead of shades:

Code

y = var('y')
contour_plot(x^2 + y^2, (x, -10, 10), (y, -10, 10), cmap='jet')

Adding a legend

We can also set colorbar to True for a legend to increase readability

Code

y = var('y')
contour_plot(x^2 + y^2, (x, -10, 10), (y, -10, 10), cmap='jet', colorbar=True)

Fill, Axes and Labels

When creating a contour plot, you can set the fill, axes and labels properties to True or False (Their default values are True, False and False, respectively). If you're setting axes or labels to True, it's a good idea to set fill to False, otherwise the fill will cover them up.

Code

y = var('y')
contour_plot(x^2 + y^2, (x, -10, 10), (y, -10, 10), fill=False, axes=True, labels=True)

Tags

Primary Tags: Plotting: Two-dimensional plots

Secondary Tags: Two-dimensional plots: Contour plots

Related Cells

Attribute

Permalink:

Author: Gregory V. Bard. Sage for Undergraduates. American Mathematical Society, Providence, RI, 2015. Available at http://www.gregorybard.com/Sage.html.

Date: 28 Feb 2019 21:50

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License