# 作图¶

Sage可以使用matplotlib、openmath、gnuplot或surf绘图，但标准发行版中Sage只包含matplotlib和openmath。有关冲浪示例，请参见 打印曲面 .

Sage中的绘图可以用许多不同的方式完成。您可以通过gnuplot绘制函数（二维或三维）或一组点（仅二维），也可以通过Maxima绘制微分方程的解（Maxima又称为gnuplot或openmath），也可以使用Singular与绘图包surf（Sage不附带）的接口。 gnuplot 没有隐式绘图命令，因此如果要使用隐式绘图绘制曲线或曲面，则最好使用Singular的界面进行浏览，如第ch:AG章“代数几何”中所述。

## 二维绘图功能¶

sage: f1 = 1
sage: f2 = 1-x
sage: f3 = exp(x)
sage: f4 = sin(2*x)
sage: f = piecewise([((0,1),f1), ((1,2),f2), ((2,3),f3), ((3,10),f4)])
sage: f.plot(x,0,10)
Graphics object consisting of 1 graphics primitive


Jacobi椭圆函数的一个红色图 $$\text{{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: show(line(L, rgbcolor=(3/4,1/4,1/8)))


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)))


Riemann-zeta函数的紫色图 $$\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)))


## 绘制曲线¶

### Mat普特利布¶

sage: L = [[1+5*cos(pi/2+pi*i/100), tan(pi/2+pi*i/100)*
....:     (1+5*cos(pi/2+pi*i/100))] for i in range(1,100)]
sage: line(L, rgbcolor=(1/4,1/8,3/4))
Graphics object consisting of 1 graphics primitive


sage: n = 4; h = 3; b = 2
sage: L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),
....:     n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
sage: line(L, rgbcolor=(1/4,1/4,3/4))
Graphics object consisting of 1 graphics primitive


sage: n = 6; h = 5; b = 2
sage: L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),
....:     n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
sage: line(L, rgbcolor=(1/4,1/4,3/4))
Graphics object consisting of 1 graphics primitive


sage: L = [[sin(pi*i/100)+sin(pi*i/50),-(1+cos(pi*i/100)+cos(pi*i/50))]
....:     for i in range(-100,101)]
sage: line(L, rgbcolor=(1,1/4,1/2))
Graphics object consisting of 1 graphics primitive


sage: L = [[2*(1-4*cos(-pi/2+pi*i/100)^2),10*tan(-pi/2+pi*i/100)*
....:     (1-4*cos(-pi/2+pi*i/100)^2)] for i in range(1,100)]
sage: line(L, rgbcolor=(1/4,1,1/8))
Graphics object consisting of 1 graphics primitive


Bernoulli的一个绿色柠檬酸盐（我们省略了i==100，因为这会产生0除法错误）：

sage: v = [(1/cos(-pi/2+pi*i/100), tan(-pi/2+pi*i/100)) for i in range(1,200) if i!=100 ]
sage: L = [(a/(a^2+b^2), b/(a^2+b^2)) for a,b in v]
sage: line(L, rgbcolor=(1/4,3/4,1/8))
Graphics object consisting of 1 graphics primitive


### 冲浪¶

sage: s = singular.eval
sage: s('LIB "surf.lib";')
...
sage: s("ring rr0 = 0,(x1,x2),dp;")
''
sage: s("ideal I = x1^3 - x2^2;")
''
sage: s("plot(I);")
...


## 开放路径¶

Openmath是由W.Schelter编写的TCL/tkgui绘图程序。

sage: maxima.plot2d('cos(2*x) + 2*exp(-x)','[x,0,1]',  # not tested (pops up a window)
....:     '[plot_format,openmath]')


（MacOSX用户：请注意 openmath 命令是在xterm shell中启动的会话中运行的，而不是使用标准的Mac终端应用程序。）

sage: maxima.eval('load("plotdf");')
'".../share/maxima/.../share/dynamics/plotdf.lisp"'
sage: maxima.eval('plotdf(x+y,[trajectory_at,2,-0.1]); ')  # not tested


sage: maxima.plot2d('[x,x^2,x^3]','[x,-1,1]','[plot_format,openmath]')  # not tested


Openmath还可以对窗体的表面进行三维打印 $$z=f(x,y)$$ 作为 $$x$$$$y$$ 矩形上的范围。例如，这里有一个“实时”3D绘图，您可以用鼠标移动它：

sage: maxima.plot3d ("sin(x^2 + y^2)", "[x, -3, 3]", "[y, -3, 3]",  # not tested
....:     '[plot_format, openmath]')


## 超光速三维绘图¶

sage: f = lambda t: (t,t^2,t^3)
sage: t = Tachyon(camera_center=(5,0,4))
sage: t.texture('t')
sage: t.light((-20,-20,40), 0.2, (1,1,1))
sage: t.parametric_plot(f,-5,5,'t',min_depth=6)


## gnuplot¶

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)


eps文件默认保存到当前目录，但如果愿意，可以指定路径。

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)


sage: maxima.plot3d_parametric(["v*sin(u)","v*cos(u)","v"], ["u","v"],
....:     [-3.2,3.2],[0,3])      # optional -- pops up a window.
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)     # optional -- pops up a window.


To illustrate how to pass gnuplot options in , here is an example of a plot of a set of points involving the Riemann zeta function $$\zeta(s)$$ (computed using Pari but plotted using Maxima and Gnuplot): {plot!points} {Riemann zeta function}

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)  # optional -- pops up a window.
sage: opts='[gnuplot_preamble, "set nokey"], [gnuplot_term, ps],
....:     [gnuplot_out_file, "zeta.eps"]'
sage: maxima.plot_list(zeta_ptsx, zeta_ptsy, opts) # optional -- pops up a window.


## 打印曲面¶

sage: singular.eval('ring rr1 = 0,(x,y,z),dp;')
''
sage: singular.eval('ideal I(1) = 2x2-1/2x3 +1-y+1;')
''
sage: singular.eval('plot(I(1));')
...