Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Blaec (CSO) Tasks and Strategy Outline.
Download

Tutorial Release 10.4 The Sage Development Team https://doc.sagemath.org/pdf/en/tutorial/sage_tutorial.pdf

Sage is free, open-source math software that supports research and teaching in algebra, geometry, number theory, cryptography, numerical computation, and related areas. Both the Sage development model and the technology in Sage itself are distinguished by an extremely strong emphasis on openness, community, cooperation, and collaboration: we are building the car, not reinventing the wheel. The overall goal of Sage is to create a viable, free, open-source alternative to Maple, Mathematica, Magma, and MATLAB.

This tutorial is the best way to become familiar with Sage in only a few hours. You can read it in HTML or PDF versions, or from the Sage notebook (click Help, then click Tutorial to interactively work through the tutorial from within Sage).

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

https://creativecommons.org/licenses/by-sa/3.0/

386 views
ubuntu2204
Kernel: SageMath 10.4

2.5 Plotting

Sage can produce two-dimensional and three-dimensional plots.

2.5.1 Two-dimensional Plots

In two dimensions, Sage can draw circles, lines, and polygons; plots of functions in rectangular coordinates; and also polar plots, contour plots, and vector field plots. We present examples of some of these here. For more examples of plotting with Sage, see Solving Differential Equations and Maxima, and also the Sage Constructions documentation.

This command produces a yellow circle of radius 1, centered at the origin:

circle((0, 0), 1, rgbcolor=(1, 1, 0))
Image in a Jupyter notebook

You can also produce a filled circle:

circle((0, 0), 1, rgbcolor=(1, 1, 0), fill=True)
Image in a Jupyter notebook

You can also create a circle by assigning it to a variable; this does not plot it:

c = circle((0, 0), 1, rgbcolor=(1, 1, 0))

To plot it, use c.show() or show(c), as follows:

c.show()
Image in a Jupyter notebook

Alternatively, evaluating c.save('filename.png') will save the plot to the given file.

Now, these ‘circles’ look more like ellipses because the axes are scaled differently. You can fix this:

from sage.all import * c.show(aspect_ratio=Integer(1))
Image in a Jupyter notebook

The command show(c, aspect_ratio=1) accomplishes the same thing, or you can save the picture using c.save(filename.png, aspect_ratio=1).

from sage.all import * plot(cos, (-Integer(5),Integer(5)))
Image in a Jupyter notebook

Once you specify a variable name, you can create parametric plots also:

from sage.all import * x = var('x') parametric_plot((cos(x),sin(x)**Integer(3)),(x,Integer(0),Integer(2)*pi),rgbcolor=hue(RealNumber(0.6)))
Image in a Jupyter notebook

It’s important to notice that the axes of the plots will only intersect if the origin is in the viewing range of the graph, and that with sufficiently large values scientific notation may be used:

from sage.all import * plot(x**Integer(2),(x,Integer(300),Integer(500)))
Image in a Jupyter notebook

You can combine several plots by adding them:

from sage.all import * x = var('x') p1 = parametric_plot((cos(x),sin(x)),(x,Integer(0),Integer(2)*pi),rgbcolor=hue(RealNumber(0.2))) p2 = parametric_plot((cos(x),sin(x)**Integer(2)),(x,Integer(0),Integer(2)*pi),rgbcolor=hue(RealNumber(0.4))) p3 = parametric_plot((cos(x),sin(x)**Integer(3)),(x,Integer(0),Integer(2)*pi),rgbcolor=hue(RealNumber(0.6))) show(p1+p2+p3, axes=False)
Image in a Jupyter notebook

A good way to produce filled-in shapes is to produce a list of points (L in the example below) and then use the polygon command to plot the shape with boundary formed by those points. For example, here is a green deltoid:

from sage.all import * L = [[-1 + cos(pi * i / 100) * (1 + cos(pi * i / 100)), 2 * sin(pi * i / 100) * (1 - cos(pi * i / 100))] for i in range(200)] p = polygon(L, rgbcolor=(1/8, 3/4, 1/2)) p
Image in a Jupyter notebook

Note: To see this without any axes, you can use show(p, axes=False).

You can add text to a plot:

L = [[6 * cos(pi * i / 100) + 5 * cos((6 / 2) * pi * i / 100), 6 * sin(pi * i / 100) - 5 * sin((6 / 2) * pi * i / 100)] for i in range(200)] p = polygon(L, rgbcolor=(1/8, 1/4, 1/2)) t = text("hypotrochoid", (5, 4), rgbcolor=(1, 0, 0)) show(p + t)
Image in a Jupyter notebook

Calculus teachers draw the following plot frequently on the board: not just one branch of arcsin\text{arcsin} but rather several of them: i.e., the plot of y=sin(x)y = \sin(x) for xx between 2π-2\pi and 2π2\pi, flipped about the 45-degree line. The following Sage commands construct this:

v = [(sin(x), x) for x in srange(-2 * float(pi), 2 * float(pi), 0.1)] line(v)
Image in a Jupyter notebook

Since the tangent function has a larger range than sine, if you use the same trick to plot the inverse tangent, you should change the minimum and maximum coordinates for the x-axis:

from sage.all import * v = [(tan(x), x) for x in srange(-2 * float(pi), 2 * float(pi), 0.01)] show(line(v), xmin=-20, xmax=20)
Image in a Jupyter notebook

Sage also computes polar plots, contour plots, and vector field plots (for special types of functions). Here is an example of a contour plot:

from sage.all import * f = lambda x, y: cos(x * y) contour_plot(f, (-4, 4), (-4, 4))
Image in a Jupyter notebook