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 sections 2.4.3 and 4.4, and also the ``Sage Constructions'' documentation.
This command produces a yellow circle of radius 1, centered at the origin:
sage: circle((0,0), 1, rgbcolor=(1,1,0))
You can also produce a filled circle:
sage: circle((0,0), 1, rgbcolor=(1,1,0), fill=True)
You can also create a circle by assigning it to a variable; this does not plot it:
sage: c = circle((0,0), 1, rgbcolor=(1,1,0))
To plot it, use c.show()
or show(c)
, as follows:
sage: c.show()
Alternatively, evaluating c.save('filename.png')
will save the
plot to the given file.
It's easy to plot basic functions:
sage: plot(cos, (-5,5))
Once you specify a variable name, you can create parametric plots also:
sage: x = var('x') sage: parametric_plot((cos(x),sin(x)^3),0,2*pi,rgbcolor=hue(0.6))
You can combine several plots by adding them:
sage: x = var('x') sage: p1 = parametric_plot((cos(x),sin(x)),0,2*pi,rgbcolor=hue(0.2)) sage: p2 = parametric_plot((cos(x),sin(x)^2),0,2*pi,rgbcolor=hue(0.4)) sage: p3 = parametric_plot((cos(x),sin(x)^3),0,2*pi,rgbcolor=hue(0.6)) sage: show(p1+p2+p3, axes=false)
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:
sage: 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)] sage: p = polygon(L, rgbcolor=(1/8,3/4,1/2)) sage: show(p)
show(p, axes=false)
to see this without any axes.
You can add text to a plot:
sage: 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)] sage: p = polygon(L, rgbcolor=(1/8,1/4,1/2)) sage: t = text("hypotrochoid", (5,4), rgbcolor=(1,0,0)) sage: g=p+t sage: show(g)
Calculus teachers draw the following plot frequently on the board: not just one branch of arcsin but rather several of them: i.e., the plot of y=sin(x) for x between –2π and 2π, flipped about the 45 degree line. The following Sage commands construct this:
sage: v = [(sin(x),x) for x in srange(-2*float(pi),2*float(pi),0.1)] sage: line(v)
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: v = [(tan(x),x) for x in srange(-2*float(pi),2*float(pi),0.01)] sage: P = line(v) sage: show(P, xmin=-20, xmax=20)
Sage also computes polar plots, contour plots and vector field plots (for special types of functions). Here is an example of a contour plot:
sage: f = lambda x,y: cos(x*y) sage: C = contour_plot(f, (-4, 4), (-4, 4)) sage: show(C)
See About this document... for information on suggesting changes.