3.1 Plotting functions in 2D

The default plotting method in Sage uses the excellent matplotlib package.

To view any of these, type P.save("<path>/myplot.png") and then open it in a graphics viewer such as gimp.

You can plot piecewise-defined functions:

 
sage: f1 = lambda x:1
sage: f2 = lambda x:1-x
sage: f3 = lambda x:exp(x)
sage: f4 = lambda x:sin(2*x)
sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
sage: P = f.plot()

Other function plots can be produced as well:

A red plot of the Jacobi elliptic function sn$ (x,2)$ , $ -3<x<3$ (do not type the ...):

 
sage: L = [(i/100.0, maxima.eval('jacobi_sn (%s/100.0,2.0)'%i))\ 
...   for i in range(-300,300)]
sage: show(line(L, rgbcolor=(3/4,1/4,1/8)))

A red plot of $ J$ -Bessel function $ J_2(x)$ , $ 0<x<10$ :

sage: L = [(i/10.0, maxima.eval('bessel_j (2,%s/10.0)'%i)) for i in range(100)]
sage: show(line(L, rgbcolor=(3/4,1/4,5/8)))

A purple plot of the Riemann zeta function $ \zeta(1/2 + it)$ , $ 0<t<30$ :

sage: I = CDF.0
sage: show(line([zeta(1/2 + k*I/6) for k in range(180)], rgbcolor=(3/4,1/2,5/8)))

See About this document... for information on suggesting changes.