标绘

Sage可以使用matplotlib、Openath、gnplot或surf进行绘图,但标准发行版中只包含matplotlib和Openath。有关冲浪示例,请参见 标绘曲面

在Sage中可以通过许多不同的方式进行计划。您可以通过GNOUUPT绘制一个函数(2维或3维)或一组点(仅限2维),您可以通过Maxima(调用Gn Uplot或OpenMATH)绘制微分方程式的解,或者您可以使用Single的界面和绘图包SURF(Sage不提供)。 gnuplot 没有隐式绘制命令,因此如果要使用隐式绘制绘制曲线或曲面,最好使用Single‘s界面进行浏览,如第ch:AG章,代数几何中所述。

2D中的打印函数

中的默认绘图方法使用卓越的 matplotlib 包裹。

要查看其中的任何内容,请键入 P.save("<path>/myplot.png") 然后在图形查看器(如GIMP)中打开它。

您可以绘制分段定义的函数:

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

还可以生成其他功能图:

雅可比椭圆函数的一个红色标图 \(\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)))

一块红色的土地 \(J\) -贝塞尔函数 \(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)))

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

绘制曲线

要在Sage中绘制曲线,可以使用Single和SURF(http://surf.sourceforge.net/,也可以作为实验包提供)或使用Matplotlib(包含在Sage中)。

matplotlib

以下是几个例子。要查看它们,请键入 p.save("<path>/my_plot.png") (其中 <path> 是您对要保存绘图的位置具有写入权限的目录路径),并在查看器(如GIMP)中查看它。

尼科米德的蓝色贝壳:

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

一条蓝色的下摆线(3片叶子):

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

一个蓝色的下摆线(4片叶子):

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

一种淡绿色的Maclaurin三叉形:

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的绿色Lemniscate(我们省略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

冲浪

尤其是,由于 surf 仅在UNIX型操作系统上可用(且不随Sage一起提供),在Sage中使用以下命令进行打印仅在此类操作系统上可用。顺便说一句,SURF包含在几个流行的Linux发行版中。

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);")
...

新闻发布会 q 在SURF窗口处于活动状态时退出SURF并返回到Sage。

您可以将此图另存为冲浪脚本。在弹出的冲浪窗口中,只需选择 filesave as ,等等。(键入 q 或选择 filequit ,以关闭窗口。)

故事情节被省略了,但我们鼓励温和的读者尝试一下。

开放数学

Openath是由W.Schelter编写的TCL/Tk图形用户界面绘图程序。

下面的命令绘制该函数 \(\cos(2x)+2e^{-x}\)

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

(Mac OS X用户:请注意,这些 openmath 在xTermShell中启动的会话中运行命令,而不是使用标准的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

这将绘制一个方向场(plotdf Maxima包也是由W.Schelter编写的)。

包含多个函数的二维曲线图:

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

OpenMATH还可以对表格中的曲面进行3D绘图 \(z=f(x,y)\) ,AS \(x\)\(y\) 范围在一个矩形上。例如,这是一个可以用鼠标移动的“实时”3D绘图:

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

通过适当地旋转,您可以查看等高线图。

快子3D标绘

光线跟踪包Tachyon随Sage一起分发。3D情节看起来非常好,但往往需要更多的设置。以下是参数空间曲线的示例:

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)

类型 t.show() 查看此内容。

《参考手册》中还有其他例子。

GNOUPUPT

你一定是 gnuplot 安装以运行这些命令。

首先,下面是绘制函数的方法:{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)

默认情况下,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)

以下是参数曲面在三维空间中的打印示例:{Plot!a参数曲面}

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)

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.

标绘曲面

To plot a surface in is no different that to plot a curve, though the syntax is slightly different. In particular, you need to have surf loaded. {plot!surface using surf}

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));')
...