Subsections


3. Plotting

SAGE can plot using gnuplot, surf, or matplotlib, but only matplotlib is included with SAGE. For surf examples, see 17.2.

3.1 gnuplot

You must have gnuplot installed to run these commands. This is an ``optional package'' which, if it isn't installed already on your machine, can be installed by typing ./sage -i gnuplot-4.0.0 on the command line in the SAGE home directory.

Plotting in SAGE can be done in many different ways. You can plot a function (in 2 or 3 dimensions) or a set of points (in 2-D only) via gnuplot (which comes with SAGE), you can plot a solution to a differential equation via Maxima (which in turn calls gnuplot), or you can use Singular's interface with the plotting package surf (which does not come with SAGE). gnuplot does not have an implicit plotting command, so if you want to plot a curve or surface using an implicit plot, it is best to use the Singular's interface to surf, as described in chapter 17, Algebraic geometry.

First, here's way to plot a function:

sage: maxima.plot2d('sin(x)','[x,-5,5]')

sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "sin-plot.eps"]'
sage: maxima.plot2d('sin(x)','[x,-5,5]',opts)
sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "/tmp/sin-plot.eps"]'
sage: maxima.plot2d('sin(x)','[x,-5,5]',opts)
The eps file is saved by default to the current directory but you may specify a path if you prefer.

Here is an example of a plot of a parametric curve in the plane:

sage: maxima.plot2d_parametric(["sin(t)","cos(t)"], "t",[-3.1,3.1])
sage: opts = '[gnuplot_preamble, "set nokey"], [gnuplot_term, ps], [gnuplot_out_file, "circle-plot.eps"]'
sage: maxima.plot2d_parametric(["sin(t)","cos(t)"], "t", [-3.1,3.1], options=opts)

Here is an example of a plot of a parametric surface in 3-space:

sage: maxima.plot3d_parametric(["v*sin(u)","v*cos(u)","v"], ["u","v"],[-3.2,3.2],[0,3])
sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "sin-cos-plot.eps"]'
sage: maxima.plot3d_parametric(["v*sin(u)","v*cos(u)","v"], ["u","v"],[-3.2,3.2],[0,3],opts)

Here is an example of a plot of a set of points involving the Riemann zeta function $ \zeta(s)$ :

sage.: zeta_ptsx = [ (pari(1/2 + i*I/10).zeta().real()).precision(1) for i in range (70,150)]  
sage.: zeta_ptsy = [ (pari(1/2 + i*I/10).zeta().imag()).precision(1) for i in range (70,150)]  
sage.: maxima.plot_list(zeta_ptsx, zeta_ptsy)                   
sage.: opts='[gnuplot_preamble, "set nokey"], [gnuplot_term, ps], [gnuplot_out_file, "zeta.eps"]'
sage.: maxima.plot_list(zeta_ptsx, zeta_ptsy, opts)

Here is an example of an interactive 3D plot which you can move with your mouse:

sage gnuplot.plot3d('sin(sin(10*x)*y5)')

Here is an example of using the gnuplot.py package gnuplot.py (gnuplot-py.sourceforge.net) directly.

sage: from Gnuplot import Gnuplot
sage: g = Gnuplot(debug=1)
sage: g.plot([[0,1.1], [1,5.8], [2,3.3], [3,4.2]])


3.2 Plotting functions using matplotlib

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$ :

 
        sage: L = [(i/100.0, maxima.eval('jacobi_sn (%s/100.0,2.0)'%i)) for i in range(-300,300)]
        sage: p = 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: p = line(L, rgbcolor=(3/4,1/4,5/8))

A purple plot of the Riemann zeta function $ \zeta(1/2 + it)$ , $ 0<t<30$ : (much better that the gnuplot version!):

        sage: L = [[(pari(1/2 + i*I/10).zeta().real()).precision(1),(pari(1/2 + i*I/10).zeta().imag()).precision(1)] for i in range (0,300)]
        sage: p = line(L, rgbcolor=(3/4,1/2,5/8))

A purple plot of the Hasse-Weil $ L$ -function $ L(E, 1 + it)$ , $ -1<t<10$ :

        sage: E = EllipticCurve('37a')
        sage: vals = E.Lseries_values_along_line(1-I, 1+10*I, 100) # critical line
        sage: L = [(z[1].real(), z[1].imag()) for z in vals]
        sage: p = line(L, rgbcolor=(3/4,1/2,5/8))

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