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.
ubuntu2204
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:
You can also produce a filled circle:
You can also create a circle by assigning it to a variable; this does not plot it:
To plot it, use c.show()
or show(c)
, as follows:
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:
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)
.
Once you specify a variable name, you can create parametric plots also:
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:
You can combine several plots by adding them:
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:
Note: To see this without any axes, you can use show(p, axes=False)
.
You can add text to a plot:
Calculus teachers draw the following plot frequently on the board: not just one branch of but rather several of them: i.e., the plot of for between and , flipped about the 45-degree line. The following Sage commands construct this:
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:
Sage also computes polar plots, contour plots, and vector field plots (for special types of functions). Here is an example of a contour plot: