Plotting Functions.

EXAMPLES:

sage: def f(x,y):
...       return math.sin(y*y+x*x)/math.sqrt(x*x+y*y+.0001)
...
sage: P = plot3d(f,(-3,3),(-3,3), adaptive=True, color=rainbow(60, 'rgbtuple'), max_bend=.1, max_depth=15)
sage: P.show()
sage: def f(x,y):
...       return math.exp(x/5)*math.sin(y)
...
sage: P = plot3d(f,(-5,5),(-5,5), adaptive=True, color=['red','yellow'])
sage: from sage.plot.plot3d.plot3d import axes
sage: S = P + axes(6, color='black')
sage: S.show()

We plot “cape man”:

sage: S = sphere(size=.5, color='yellow')
sage: from sage.plot.plot3d.shapes import Cone
sage: S += Cone(.5, .5, color='red').translate(0,0,.3)
sage: S += sphere((.45,-.1,.15), size=.1, color='white') + sphere((.51,-.1,.17), size=.05, color='black')
sage: S += sphere((.45, .1,.15),size=.1, color='white') + sphere((.51, .1,.17), size=.05, color='black')
sage: S += sphere((.5,0,-.2),size=.1, color='yellow')
sage: def f(x,y): return math.exp(x/5)*math.cos(y)
sage: P = plot3d(f,(-5,5),(-5,5), adaptive=True, color=['red','yellow'], max_depth=10)
sage: cape_man = P.scale(.2) + S.translate(1,0,0)
sage: cape_man.show(aspect_ratio=[1,1,1])

AUTHORS:

  • Tom Boothby: adaptive refinement triangles
  • Josh Kantor: adaptive refinement triangles
  • Robert Bradshaw (2007-08): initial version of this file
  • William Stein (2007-12, 2008-01): improving 3d plotting
class sage.plot.plot3d.plot3d.TrivialTriangleFactory
smooth_triangle(a, b, c, da, db, dc, color=None)
triangle(a, b, c, color=None)
sage.plot.plot3d.plot3d.axes(scale=1, radius=None, **kwds)
sage.plot.plot3d.plot3d.plot3d(f, urange, vrange, adaptive=False, **kwds)

INPUT:

  • f - a symbolic expression or function of 2 variables
  • urange - a 2-tuple (u_min, u_max) or a 3-tuple (u, u_min, u_max)
  • vrange - a 2-tuple (v_min, v_max) or a 3-tuple (v, v_min, v_max)
  • adaptive - (default: False) whether to use adaptive refinement to draw the plot (slower, but may look better)
  • mesh - bool (default: False) whether to display mesh grid lines
  • dots - bool (default: False) whether to display dots at mesh grid points

Note

mesh and dots are not supported when using the Tachyon raytracer renderer.

EXAMPLES: We plot a 3d function defined as a Python function:

sage: plot3d(lambda x, y: x^2 + y^2, (-2,2), (-2,2))

We plot the same 3d function but using adaptive refinement:

sage: plot3d(lambda x, y: x^2 + y^2, (-2,2), (-2,2), adaptive=True)

Adaptive refinement but with more points:

sage: plot3d(lambda x, y: x^2 + y^2, (-2,2), (-2,2), adaptive=True, initial_depth=5)

We plot some 3d symbolic functions:

sage: x, y = var('x,y')
sage: plot3d(x^2 + y^2, (x,-2,2), (y,-2,2))
sage: plot3d(sin(x*y), (x, -pi, pi), (y, -pi, pi))

A 3d plot with a mesh:

sage: var('x,y')
(x, y)
sage: plot3d(sin(x-y)*y*cos(x),(x,-3,3),(y,-3,3), mesh=True)     

Two wobby translucent planes:

sage: x,y = var('x,y')
sage: P = plot3d(x+y+sin(x*y), (x,-10,10),(y,-10,10), opacity=0.87, color='blue')
sage: Q = plot3d(x-2*y-cos(x*y),(x,-10,10),(y,-10,10),opacity=0.3,color='red')
sage: P + Q

We draw two parametric surfaces and a transparent plane:

sage: L = plot3d(lambda x,y: 0, (-5,5), (-5,5), color="lightblue", opacity=0.8)
sage: P = plot3d(lambda x,y: 4 - x^3 - y^2, (-2,2), (-2,2), color='green')
sage: Q = plot3d(lambda x,y: x^3 + y^2 - 4, (-2,2), (-2,2), color='orange')
sage: L + P + Q

We draw the “Sinus” function (water ripple-like surface):

sage: x, y = var('x y')
sage: plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1))

Hill and valley (flat surface with a bump and a dent):

sage: x, y = var('x y')
sage: plot3d( 4*x*exp(-x^2-y^2), (x,-2,2), (y,-2,2))

TESTS: Listing the same plot variable twice gives an error.

sage: x, y = var('x y')
sage: plot3d( 4*x*exp(-x^2-y^2), (x,-2,2), (x,-2,2))
...
ValueError: plot variables should be distinct, but both are x.
sage.plot.plot3d.plot3d.plot3d_adaptive(f, x_range, y_range, color='automatic', grad_f=None, max_bend=0.5, max_depth=5, initial_depth=4, num_colors=128, **kwds)

Adaptive 3d plotting of a function of two variables.

This is used internally by the plot3d command when the option adaptive=True is given.

INPUT:

  • f - a symbolic function or a Python function of 3 variables.
  • x_range - x range of values: 2-tuple (xmin, xmax) or 3-tuple (x,xmin,xmax)
  • y_range - y range of values: 2-tuple (ymin, ymax) or 3-tuple (y,ymin,ymax)
  • grad_f - gradient of f as a Python function
  • color - “automatic” - a rainbow of num_colors colors
  • num_colors - (default: 128) number of colors to use with default color
  • max_bend - (default: 0.5)
  • max_depth - (default: 5)
  • initial_depth - (default: 4)
  • **kwds - standard graphics parameters

EXAMPLES: We plot \sin(xy):

sage: from sage.plot.plot3d.plot3d import plot3d_adaptive
sage: x,y=var('x,y'); plot3d_adaptive(sin(x*y), (x,-pi,pi), (y,-pi,pi), initial_depth=5)

Previous topic

List Plots

Next topic

Platonic Solids.

This Page