# 高级二维打印教程¶

## 绘制函数和绘制曲线¶

### 笛卡尔图¶

sage: plot(x^2, (x,-2,2))
Graphics object consisting of 1 graphics primitive


sage: regular = plot(x^2, (x,-2,2), color= 'purple')
sage: skinny = plot(4*x^2, (x,-2,2), color = 'green')
sage: regular + skinny
Graphics object consisting of 2 graphics primitives


sage: plot(1+e^(-x^2), xmin=-2, xmax=2, ymin=0, ymax=2.5, figsize=10)
Graphics object consisting of 1 graphics primitive


sage: exponential = plot(1+e^(-x^2), xmin=-2, xmax=2, ymin=0, ymax=2.5)
sage: max_line = plot(2, xmin=-2, xmax=2, linestyle='-.', color = 'red')
sage: min_line = plot(1, xmin=-2, xmax=2, linestyle=':', color = 'red')
sage: exponential + max_line + min_line
Graphics object consisting of 3 graphics primitives


sage: exponential = plot(1+e^(-x^2), xmin=-2, xmax=2, ymin=0, ymax=2.5, fill=0.5, fillcolor='grey', fillalpha=0.3)
sage: min_line = plot(1, xmin=-2, xmax=2, linestyle='-', thickness= 6, color = 'red')
sage: exponential + min_line
Graphics object consisting of 3 graphics primitives

sage: sum([plot(x^n,(x,0,1),color=rainbow(5)[n]) for n in [0..4]])
Graphics object consisting of 5 graphics primitives


### 参数化绘图¶

sage: t = var('t')
sage: parametric_plot([cos(t) + 3 * cos(t/9), sin(t) - 3 * sin(t/9)], (t, 0, 18*pi), fill = True, aspect_ratio=1)
Graphics object consisting of 2 graphics primitives


$x（t）=17cos（t）+3cos（17t/3）$
$y（t）=17sin（t）-3sin（17t/3）$

Sage自动打印二维或三维打印，以及曲线或曲面，具体取决于指定的变量和坐标的数量。

sage: t = var('t')
sage: parametric_plot((t^2,sin(t)), (t,0,pi))
Graphics object consisting of 1 graphics primitive

sage: parametric_plot((t^2,sin(t),cos(t)), (t,0,pi))
Graphics3d Object

sage: r = var('r')
sage: parametric_plot((t^2,sin(r*t),cos(r*t)), (t,0,pi),(r,-1,1))
Graphics3d Object


### 极坐标图¶

Sage也可以做极坐标图。

sage: polar_plot(2 + 2*cos(x), (x, 0, 2*pi), color=hue(0.5), thickness=4)
Graphics object consisting of 1 graphics primitive


sage: t = var('t')
sage: polar_plot([cos(4*t) + 1.5,  0.5 * cos(4*t) + 2.5], (t, 0, 2*pi),
....:            color='black', thickness=2, fill=True, fillcolor='orange')
Graphics object consisting of 4 graphics primitives


### 交互式演示¶

sage: html('<h2>Sine and unit circle (by Jurgis Pralgauskis)</h2> inspired by <a href="http://www.youtube.com/watch?v=Ohp6Okk_tww&feature=related">this video</a>' )
sage: # http://doc.sagemath.org/html/en/reference/sage/plot/plot.html
sage: graph_params = dict(xmin = -2*radius,    xmax = 360,
....:                    aspect_ratio=1,
....:                    axes = False
....:                    )
sage: def sine_and_unit_circle( angle=30, instant_show = True, show_pi=True ):
....:     ccenter_x, ccenter_y = -radius, 0  # center of cirlce on real coords
....:     sine_x = angle # the big magic to sync both graphs :)
....:     current_y = circle_y = sine_y = radius * sin(angle*pi/180)
....:     circle_x = ccenter_x + radius * cos(angle*pi/180)
....:     graph = Graphics()
....:     # we'll put unit circle and sine function on the same graph
....:     # so there will be some coordinate mangling ;)
....:     # CIRCLE
....:     unit_circle = circle((ccenter_x, ccenter_y), radius, color="#ccc")
....:     # SINE
....:     x = var('x')
....:     sine = plot( radius * sin(x*pi/180) , (x, 0, 360), color="#ccc" )
....:     graph += unit_circle + sine
....:     # CIRCLE axis
....:     # x axis
....:     graph +=  arrow( [-2*radius, 0], [0, 0], color = "#666" )
....:     graph += text("$(1, 0)$",  [-16, 16],  color = "#666")
....:     # circle y axis
....:     graph += text("$(0, 1)$",  [ccenter_x, radius+15],  color = "#666")
....:     # circle center
....:     graph += text("$(0, 0)$",  [ccenter_x, 0],  color = "#666")
....:     # SINE x axis
....:     graph +=  arrow( [0,0], [360, 0], color = "#000" )
....:     # let's set tics
....:     # or http://aghitza.org/posts/tweak_labels_and_ticks_in_2d_plots_using_matplotlib/
....:     # or wayt for http://trac.sagemath.org/sage_trac/ticket/1431
....:     # ['$-\pi/3$', '$2\pi/3$', '$5\pi/3$']
....:     for x in range(0, 361, 30):
....:         graph += point( [x, 0] )
....:         angle_label = ".  $%3d^{\circ}$ " % x
....:         if show_pi: angle_label += " $(%s\pi)$"% x/180
....:         graph += text(angle_label,  [x, 0], rotation=-90,
....:         vertical_alignment='top', fontsize=8, color="#000" )
....:     # CURRENT VALUES
....:     # SINE -- y
....:     graph +=  arrow( [sine_x,0], [sine_x, sine_y], width=1, arrowsize=3)
....:     graph +=  arrow( [circle_x,0], [circle_x, circle_y], width=1, arrowsize=3)
....:     graph +=  line(([circle_x, current_y], [sine_x, current_y]), rgbcolor="#0F0", linestyle = "--", alpha=0.5)
....:     # LABEL on sine
....:     graph += text("$(%d^{\circ}, %3.2f)$"%(sine_x, float(current_y)/radius),  [sine_x+30, current_y],  color = "#0A0")
....:     # ANGLE -- x
....:     # on sine
....:     graph += arrow( [0,0], [sine_x, 0], width=1, arrowsize=1, color='red')
....:     # on circle
....:     graph += disk( (ccenter_x, ccenter_y), float(radius)/4, (0, angle*pi/180), color='red', fill=False, thickness=1)
....:     graph +=  arrow( [ccenter_x, ccenter_y], [circle_x, circle_y],
....:                  rgbcolor="#cccccc", width=1, arrowsize=1)
....:     if instant_show:
....:         show (graph,  **graph_params)
....:     return graph
sage: #####################
sage: # make Interaction
sage: ######################
sage: @interact
sage: def _( angle = slider([0..360], default=30, step_size=5,
....:          label="Pasirinkite kampą:    ", display_value=True) ):
....:     sine_and_unit_circle(angle, show_pi = False)


## 绘制图形¶

### 绘制数据点¶

$Fn=frac{1}{sqrt{5}}左（frac{1+sqrt{5}}{2}右）^n，$

sage: fibonacci_sequence(6)
<generator object fibonacci_sequence at ...>

sage: list(fibonacci_sequence(6))
[0, 1, 1, 2, 3, 5]


sage: list(enumerate(fibonacci_sequence(6)))
[(0, 0), (1, 1), (2, 1), (3, 2), (4, 3), (5, 5)]


sage: fibonacci = list(enumerate(fibonacci_sequence(6)))
sage: f(n)=(1/sqrt(5))*((1+sqrt(5))/2)^n
sage: asymptotic = [(i, f(i)) for i in range(6)]
sage: fibonacci
[(0, 0), (1, 1), (2, 1), (3, 2), (4, 3), (5, 5)]
sage: asymptotic
[(0, 1/5*sqrt(5)), (1, 1/10*sqrt(5)*(sqrt(5) + 1)), (2, 1/20*sqrt(5)*(sqrt(5) + 1)^2), (3, 1/40*sqrt(5)*(sqrt(5) + 1)^3), (4, 1/80*sqrt(5)*(sqrt(5) + 1)^4), (5, 1/160*sqrt(5)*(sqrt(5) + 1)^5)]


sage: fib_plot=list_plot(fibonacci, color='red', pointsize=30)
sage: asy_plot = list_plot(asymptotic, marker='D',color='black',thickness=2,plotjoined=True)
sage: show(fib_plot+asy_plot, aspect_ratio=1)


sage: fib_plot=scatter_plot(fibonacci, facecolor='red', marker='o',markersize=40)
sage: asy_plot = line(asymptotic, marker='D',color='black',thickness=2)
sage: show(fib_plot+asy_plot, aspect_ratio=1)


## 等高线图¶

### 等高线图¶

sage: f(x,y)=y^2+1-x^3-x
sage: contour_plot(f, (x,-pi,pi), (y,-pi,pi))
Graphics object consisting of 1 graphics primitive


sage: contour_plot(f, (x,-pi,pi), (y,-pi,pi),colorbar=True,labels=True)
Graphics object consisting of 1 graphics primitive


sage: contour_plot(f, (x,-pi,pi), (y,-pi,pi), contours=[-4,0,4], fill=False,
....:     cmap='cool', labels=True, label_inline=True,
....:     label_fmt={-4:"low", 0:"medium", 4: "hi"}, label_colors='black')
Graphics object consisting of 1 graphics primitive


sage: f(x,y)
-x^3 + y^2 - x + 1

sage: implicit_plot(f(x,y)==0,(x,-pi,pi),(y,-pi,pi))
Graphics object consisting of 1 graphics primitive


sage: density_plot(f, (x, -2, 2), (y, -2, 2))
Graphics object consisting of 1 graphics primitive


sage: density_plot(f,(x,-2,2),(y,-2,2))+contour_plot(f,(x,-2,2),(y,-2,2),fill=False,labels=True,label_inline=True,cmap='jet')
Graphics object consisting of 2 graphics primitives


• 它们以“属性”的形式给出-无括号 contour_plot 对象。

• 它们是作为字典提供的（参见 the programming tutorial

sage: contour_plot.options
{'aspect_ratio': 1,
'axes': False,
'colorbar': False,
'contours': None,
'fill': True,
'frame': True,
'labels': False,
'legend_label': None,
'linestyles': None,
'linewidths': None,
'plot_points': 100,
'region': None}


sage: contour_plot.options["fill"]=False
sage: contour_plot.options
{'aspect_ratio': 1,
'axes': False,
'colorbar': False,
'contours': None,
'fill': False,
'frame': True,
'labels': False,
'legend_label': None,
'linestyles': None,
'linewidths': None,
'plot_points': 100,
'region': None}


sage: contour_plot(f,(x,-2,2),(y,-2,2))
Graphics object consisting of 1 graphics primitive


sage: contour_plot.defaults()
{'aspect_ratio': 1,
'axes': False,
'colorbar': False,
'contours': None,
'fill': True,
'frame': True,
'labels': False,
'legend_label': None,
'linestyles': None,
'linewidths': None,
'plot_points': 100,
'region': None}


### 矢量场¶

sage: var('x,y')
(x, y)
sage: plot_vector_field((-y+x,y*x),(x,-3,3),(y,-3,3))
Graphics object consisting of 1 graphics primitive

sage: var('x,y,z')
(x, y, z)
sage: plot_vector_field3d((-y,-z,x), (x,-3,3),(y,-3,3),(z,-3,3))
Graphics3d Object


### 复杂地块¶

sage: f(z) = exp(z) #z^5 + z - 1 + 1/z
sage: complex_plot(f, (-5,5),(-5,5))
Graphics object consisting of 1 graphics primitive


### 区域图¶

sage: region_plot(cos(x^2+y^2) <= 0, (x, -3, 3), (y, -3, 3),aspect_ratio=1)
Graphics object consisting of 1 graphics primitive


sage: region_plot(sin(x)*sin(y) >= 1/4, (x,-10,10), (y,-10,10), incol='yellow', bordercol='black', borderstyle='dashed', plot_points=250,aspect_ratio=1)
Graphics object consisting of 2 graphics primitives


## 其他绘图信息¶

### 内置图形对象¶

Sage包括各种内置图形对象。对于在绘图中添加某些难以用方程描述的对象，但这些对象仍然是基本的几何对象，这些对象特别有用。在这一节中，我们将尝试演示其中一些最有用的语法；对于其中的大多数，上下文（记住，附加 ? )帮助会提供更多细节。

#### 点¶

sage: point((3,5))
Graphics object consisting of 1 graphics primitive


sage: f(x)=x^2
sage: points([(0,f(0)), (1,f(1)), (2,f(2)), (3,f(3)), (4,f(4))])
Graphics object consisting of 1 graphics primitive

sage: points([(x,f(x)) for x in range(5)])
Graphics object consisting of 1 graphics primitive


Sage会自动告诉你有多少维度在工作。

sage: f(x,y)=x^2-y^2
sage: points([(x,y,f(x,y)) for x in range(5) for y in range(5)])
Graphics3d Object


#### 线¶

sage: f(x)=x^2
sage: line([(x,f(x)) for x in range(5)])
Graphics object consisting of 1 graphics primitive


#### 球¶

Sage有各种类型的磁盘和球体。通常只需要中心和半径，但也可以选择其他选项。

sage: circle((0,1),1,aspect_ratio=1)
Graphics object consisting of 1 graphics primitive

sage: disk((0,0), 1, (pi, 3*pi/2), color='yellow',aspect_ratio=1)
Graphics object consisting of 1 graphics primitive


#### 箭头¶

sage: arrow((0,0), (1,1))
Graphics object consisting of 1 graphics primitive


#### 多边形¶

sage: polygon([[0,0],[1,1],[1,2]])
Graphics object consisting of 1 graphics primitive


#### 文本¶

sage: text(r'$\int_0^2 x^2\, dx$', (0.5,2))+plot(x^2,(x,0,2),fill=True)
Graphics object consisting of 3 graphics primitives


### 保存绘图¶

sage: p=plot(x^2,(x,-1,1))
sage: p
Graphics object consisting of 1 graphics primitive


sage: name = tmp_filename() # this is a string
sage: png_savename = name+'.png'
sage: p.save(png_savename)


sage: pdf_savename = name+'.pdf'
sage: p.save(pdf_savename)


sage: svg_savename = name+'.svg'
sage: p.save(svg_savename)