The ``Constructions'' Sage documentation has some examples of
using Sage for plotting, as do sections 2.10.3 and
4.4 below. We shall give some other examples here
of using matplotlib. To view any one of these, either use the
show command or, after entering the
commands below for the picture you want, type
p.save("<path>/my_plot.png") and view the plot
in a graphics viewer such as GIMP. In some of the examples
we had to break the long lines into multilines using a
backslash; do not type the ... on the following lines, as
they are generated by Sage to indicate multiple lines.
Here's a yellow circle:
sage: L = [[cos(pi*i/100),sin(pi*i/100)] for i in range(200)] sage: p = polygon(L, rgbcolor=(1,1,0)) sage: p.save() ## or p.show()
A green deltoid (do not type the ...):
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: p.save()
p.show(axes=false) to see this without any axes.
A blue figure 8 (do not type the ...):
sage: L = [[2*cos(pi*i/100)*sqrt(1-sin(pi*i/100)^2),\ ... 2*sin(pi*i/100)*sqrt(1-sin(pi*i/100)^2)] for i in range(200)] sage: p = polygon(L, rgbcolor=(1/8,1/4,1/2)) sage: p.save()
A blue hypotrochoid (do not type the ...):
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: p.save()
A purple epicycloid (do not type the ...):
sage: m = 9; b = 1 sage: L = [[m*cos(pi*i/100)+b*cos((m/b)*pi*i/100),\ ... m*sin(pi*i/100)-b*sin((m/b)*pi*i/100)] for i in range(200)] sage: p = polygon(L, rgbcolor=(7/8,1/4,3/4)) sage: p.save()
A blue 8-leaved petal (do not type the ...):
sage: L = [[sin(5*pi*i/100)^2*cos(pi*i/100)^3,\ ... sin(5*pi*i/100)^2*sin(pi*i/100)] for i in range(200)] sage: p = polygon(L, rgbcolor=(1/3,1/2,3/5)) sage: p.save()
You can also add text to a plot:
sage: L = [[cos(pi*i/100)^3,sin(pi*i/100)] for i in range(200)]
sage: p = line(L, rgbcolor=(1/4,1/8,3/4))
sage: t = text("a bulb", (-1.7, 0.5))
sage: x = text("x axis", (2,-0.2))
sage: y = text("y axis", (0.6,1.3))
sage: g = p+t+x+y
sage: g.save(xmin=-1.5, xmax=2, ymin=-1, ymax=1.3)
g.show(xmin=-1.5, xmax=2, ymin=-1, ymax=1.3) can be used to
view this.
You can add plots:
sage: x = var('x')
sage: p3 = parametric_plot((cos(x),sin(x)^3),0,2*pi,rgbcolor=hue(0.6))
sage: p2 = parametric_plot((cos(x),sin(x)^2),0,2*pi,rgbcolor=hue(0.4))
sage: p1 = parametric_plot((cos(x),sin(x)),0,2*pi,rgbcolor=hue(0.2))
sage: (p1+p2+p3).save(axes=false)
show(p1+p2+p3,axes=false) can be used to
view this.
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
over say
to
, flipped about the
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: P = line(v)
show(P, xmin=-2, xmax=2) can be used to
view this.
Since the tangent function has a larger range, for the inverse tangent, the show command should be modified to create a better image:
sage: v = [(tan(x),x) for x in srange(-2*float(pi),2*float(pi),0.1)] sage: P = line(v)
sage: show(P, xmin=-20, xmax=20)
can be used to create a good view of this.
Note we used float(pi) since the iteration through the
range of inputs is very fast.
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) can be used to
view this.
See About this document... for information on suggesting changes.