Plotting fields

class sage.plot.plot_field.PlotField(xpos_array, ypos_array, xvec_array, yvec_array, options)[source]

Bases: GraphicPrimitive

Primitive class that initializes the PlotField graphics type

get_minmax_data()[source]

Return a dictionary with the bounding box data.

EXAMPLES:

sage: x,y = var('x,y')
sage: d = plot_vector_field((.01*x,x+y), (x,10,20), (y,10,20))[0].get_minmax_data()
sage: d['xmin']
10.0
sage: d['ymin']
10.0
>>> from sage.all import *
>>> x,y = var('x,y')
>>> d = plot_vector_field((RealNumber('.01')*x,x+y), (x,Integer(10),Integer(20)), (y,Integer(10),Integer(20)))[Integer(0)].get_minmax_data()
>>> d['xmin']
10.0
>>> d['ymin']
10.0
sage.plot.plot_field.plot_slope_field(f, xrange, yrange, **kwds)[source]

plot_slope_field takes a function of two variables xvar and yvar (for instance, if the variables are \(x\) and \(y\), take \(f(x,y)\)), and at representative points \((x_i,y_i)\) between xmin, xmax, and ymin, ymax respectively, plots a line with slope \(f(x_i,y_i)\) (see below).

plot_slope_field(f, (xvar,xmin,xmax), (yvar,ymin,ymax))

EXAMPLES:

A logistic function modeling population growth:

sage: x,y = var('x y')
sage: capacity = 3 # thousand
sage: growth_rate = 0.7 # population increases by 70% per unit of time
sage: plot_slope_field(growth_rate * (1-y/capacity) * y, (x,0,5), (y,0,capacity*2))
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> x,y = var('x y')
>>> capacity = Integer(3) # thousand
>>> growth_rate = RealNumber('0.7') # population increases by 70% per unit of time
>>> plot_slope_field(growth_rate * (Integer(1)-y/capacity) * y, (x,Integer(0),Integer(5)), (y,Integer(0),capacity*Integer(2)))
Graphics object consisting of 1 graphics primitive
../../_images/plot_field-1.svg

Plot a slope field involving sin and cos:

sage: x,y = var('x y')
sage: plot_slope_field(sin(x+y) + cos(x+y), (x,-3,3), (y,-3,3))
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> x,y = var('x y')
>>> plot_slope_field(sin(x+y) + cos(x+y), (x,-Integer(3),Integer(3)), (y,-Integer(3),Integer(3)))
Graphics object consisting of 1 graphics primitive
../../_images/plot_field-2.svg

Plot a slope field using a lambda function:

sage: plot_slope_field(lambda x,y: x + y, (-2,2), (-2,2))
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> plot_slope_field(lambda x,y: x + y, (-Integer(2),Integer(2)), (-Integer(2),Integer(2)))
Graphics object consisting of 1 graphics primitive
../../_images/plot_field-3.svg
sage.plot.plot_field.plot_vector_field(f_g, xrange, yrange, plot_points=20, frame=True, **options)[source]

plot_vector_field takes two functions of two variables xvar and yvar (for instance, if the variables are \(x\) and \(y\), take \((f(x,y), g(x,y))\)) and plots vector arrows of the function over the specified ranges, with xrange being of xvar between xmin and xmax, and yrange similarly (see below).

plot_vector_field((f,g), (xvar,xmin,xmax), (yvar,ymin,ymax))

EXAMPLES:

Plot some vector fields involving sin and cos:

sage: x,y = var('x y')
sage: plot_vector_field((sin(x),cos(y)), (x,-3,3), (y,-3,3))
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> x,y = var('x y')
>>> plot_vector_field((sin(x),cos(y)), (x,-Integer(3),Integer(3)), (y,-Integer(3),Integer(3)))
Graphics object consisting of 1 graphics primitive
../../_images/plot_field-4.svg
sage: plot_vector_field((y,(cos(x)-2) * sin(x)), (x,-pi,pi), (y,-pi,pi))
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> plot_vector_field((y,(cos(x)-Integer(2)) * sin(x)), (x,-pi,pi), (y,-pi,pi))
Graphics object consisting of 1 graphics primitive
../../_images/plot_field-5.svg

Plot a gradient field:

sage: u, v = var('u v')
sage: f = exp(-(u^2 + v^2))
sage: plot_vector_field(f.gradient(), (u,-2,2), (v,-2,2), color='blue')
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> u, v = var('u v')
>>> f = exp(-(u**Integer(2) + v**Integer(2)))
>>> plot_vector_field(f.gradient(), (u,-Integer(2),Integer(2)), (v,-Integer(2),Integer(2)), color='blue')
Graphics object consisting of 1 graphics primitive
../../_images/plot_field-6.svg

Plot two orthogonal vector fields:

sage: x,y = var('x,y')
sage: a = plot_vector_field((x,y), (x,-3,3), (y,-3,3), color='blue')
sage: b = plot_vector_field((y,-x), (x,-3,3), (y,-3,3), color='red')
sage: show(a + b)
>>> from sage.all import *
>>> x,y = var('x,y')
>>> a = plot_vector_field((x,y), (x,-Integer(3),Integer(3)), (y,-Integer(3),Integer(3)), color='blue')
>>> b = plot_vector_field((y,-x), (x,-Integer(3),Integer(3)), (y,-Integer(3),Integer(3)), color='red')
>>> show(a + b)
../../_images/plot_field-7.svg

We ignore function values that are infinite or NaN:

sage: x,y = var('x,y')
sage: plot_vector_field((-x/sqrt(x^2+y^2),-y/sqrt(x^2+y^2)), (x,-10,10), (y,-10,10))
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> x,y = var('x,y')
>>> plot_vector_field((-x/sqrt(x**Integer(2)+y**Integer(2)),-y/sqrt(x**Integer(2)+y**Integer(2))), (x,-Integer(10),Integer(10)), (y,-Integer(10),Integer(10)))
Graphics object consisting of 1 graphics primitive
../../_images/plot_field-8.svg
sage: x,y = var('x,y')
sage: plot_vector_field((-x/sqrt(x+y),-y/sqrt(x+y)), (x,-10, 10), (y,-10,10))
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> x,y = var('x,y')
>>> plot_vector_field((-x/sqrt(x+y),-y/sqrt(x+y)), (x,-Integer(10), Integer(10)), (y,-Integer(10),Integer(10)))
Graphics object consisting of 1 graphics primitive
../../_images/plot_field-9.svg

Extra options will get passed on to show(), as long as they are valid:

sage: plot_vector_field((x,y), (x,-2,2), (y,-2,2), xmax=10)
Graphics object consisting of 1 graphics primitive
sage: plot_vector_field((x,y), (x,-2,2), (y,-2,2)).show(xmax=10) # These are equivalent
>>> from sage.all import *
>>> plot_vector_field((x,y), (x,-Integer(2),Integer(2)), (y,-Integer(2),Integer(2)), xmax=Integer(10))
Graphics object consisting of 1 graphics primitive
>>> plot_vector_field((x,y), (x,-Integer(2),Integer(2)), (y,-Integer(2),Integer(2))).show(xmax=Integer(10)) # These are equivalent
../../_images/plot_field-10.svg