Sage Reference: Plotting


Simple 2D Plots

Two dimensional graphs can be created in Sage using the plot command:

To refine the area shown, you can specify the interval of the horizontal axis that should be displayed:

Plot Options

There are several options you can specify to make your plot look exactly as you’d like. You can use as many (or few) of these options at once as needed. For a list of options, evaluate the cell below:

Color can be added to a graph by using common color names:
or by specifying an rgb color combination:

To change the thickness of a graph, set an integer value greater than 1:

The style of the graph can be set to 'solid', 'dashed', 'dashdot', 'dotted', or 'None':

To hide the axes, set the option to False. Titles can also be added:

Predefined Plots

Sage includes a number of functions for plotting common shapes, such as circles and lines.

To plot a circle, specify a center and radius. You can also decide if you'd like the circle to be filled, with options for the fill color and edge color. Below, a circle with radius 2 is centered at the point (3,5), and some fancy styling options have been applied:

Ellipses can be created by providing a center point, two (positive) numbers for the radii, and an angle:

Points can be added to a graph by providing a list of coordinates:

Line segments can be drawn by providing two points the line should pass through:

The line command can also be used to connect multiple points, to approximate curves and define polygons. The cell below defines a set L of points whose x coordinate ranges from 0 to 50, increasing by 2, and whose corresponding y coordinate is given by \( \cos(x/\pi)\). It then creates a line connecting the points of the set by segments, in the order they are defined in the set:

Polygons can be created using a list of points, as above:

Sage also has functions of interest to graph theorists:

Multiple Plots

To plot multiple functions on a single grid, assign names to the plots and use the show method to put them together:

Combining several plots and plot options allows for some great pictures:

3D Plots (Requires Java)

When creating a graph of a 3D function, you'll need to define any additional variables before defining your function. After that, the idea is similar to 2D plots, except you'll use the plot3d method instead of plot:

We can also make some simple 3D shapes, like cubes:
and even platonic solids:

Data

Sage also includes commands for creating bar charts:
as well as matrix plots:

Lists of points can be used to generate scatterplots. Below a list of random points on the interval \([0,10)\):

By applying options, two scatterplots can be combined for comparison:

Slope and Vector Fields

Slope field can be generated by providing a function in two variables:
while vector fields can be generated by providing two functions in two variables:
Gradient fields are plotted by using the vector_field command: