Primitive class for the contour plot graphics type. See contour_plot? for help actually doing contour plots.
INPUT:
EXAMPLES:
Note this should normally be used indirectly via contour_plot:
sage: from sage.plot.contour_plot import ContourPlot
sage: C = ContourPlot([[1,3],[2,4]],(1,2),(2,3),options={})
sage: C
ContourPlot defined by a 2 x 2 data grid
sage: C.xrange
(1, 2)
TESTS:
We test creating a contour plot:
sage: x,y = var('x,y')
sage: C = contour_plot(x^2-y^3+10*sin(x*y), (x, -4, 4), (y, -4, 4),plot_points=121,cmap='hsv')
Initializes base class ContourPlot.
EXAMPLES:
sage: x,y = var('x,y')
sage: C = contour_plot(x^2-y^3+10*sin(x*y), (x, -4, 4), (y, -4, 4),plot_points=121,cmap='hsv')
sage: C[0].xrange
(-4.0, 4.0)
sage: C[0].options()['plot_points']
121
Return the allowed options for the ContourPlot class.
EXAMPLES:
sage: x,y = var('x,y')
sage: C = contour_plot(x^2-y^2,(x,-2,2),(y,-2,2))
sage: isinstance(C[0]._allowed_options(),dict)
True
TESTS:
A somewhat random plot, but fun to look at:
sage: x,y = var('x,y')
sage: contour_plot(x^2-y^3+10*sin(x*y), (x, -4, 4), (y, -4, 4),plot_points=121,cmap='hsv')
String representation of ContourPlot primitive.
EXAMPLES:
sage: x,y = var('x,y')
sage: C = contour_plot(x^2-y^2,(x,-2,2),(y,-2,2))
sage: c = C[0]; c
ContourPlot defined by a 25 x 25 data grid
Returns a dictionary with the bounding box data.
EXAMPLES:
sage: x,y = var('x,y')
sage: f(x,y) = x^2 + y^2
sage: d = contour_plot(f, (3, 6), (3, 6))[0].get_minmax_data()
sage: d['xmin']
3.0
sage: d['ymin']
3.0
contour_plot takes a function of two variables,
and plots contour lines of the function over the specified
xrange and yrange as demonstrated below.
contour_plot(f, (xmin, xmax), (ymin, ymax), ...)
INPUT:
The following inputs must all be passed in as named parameters:
EXAMPLES:
Here we plot a simple function of two variables. Note that since the input function is an expression, we need to explicitly declare the variables in 3-tuples for the range:
sage: x,y = var('x,y')
sage: contour_plot(cos(x^2+y^2), (x, -4, 4), (y, -4, 4))
Here we change the ranges and add some options:
sage: x,y = var('x,y')
sage: contour_plot((x^2)*cos(x*y), (x, -10, 5), (y, -5, 5), fill=False, plot_points=100)
An even more complicated plot:
sage: x,y = var('x,y')
sage: contour_plot(sin(x^2 + y^2)*cos(x)*sin(y), (x, -4, 4), (y, -4, 4),plot_points=100)
Some elliptic curves, but with symbolic endpoints. In the first example, the plot is rotated 90 degrees because we switch the variables x,y:
sage: x,y = var('x,y')
sage: contour_plot(y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi))
sage: contour_plot(y^2 + 1 - x^3 - x, (x,-pi,pi), (y,-pi,pi))
We can play with the contour levels:
sage: x,y = var('x,y')
sage: f(x,y) = x^2 + y^2
sage: contour_plot(f, (-2, 2), (-2, 2))
sage: contour_plot(f, (-2, 2), (-2, 2), contours=2, cmap=[(1,0,0), (0,1,0), (0,0,1)])
sage: contour_plot(f, (-2, 2), (-2, 2), contours=(0.1, 1.0, 1.2, 1.4), cmap='hsv')
sage: contour_plot(f, (-2, 2), (-2, 2), contours=(1.0,), fill=False)
This should plot concentric circles centered at the origin:
sage: x,y = var('x,y')
sage: contour_plot(x^2+y^2-2,(x,-1,1), (y,-1,1)).show(aspect_ratio=1)
Extra options will get passed on to show(), as long as they are valid:
sage: f(x, y) = cos(x) + sin(y)
sage: contour_plot(f, (0, pi), (0, pi), axes=False)
sage: contour_plot(f, (0, pi), (0, pi)).show(axes=False) # These are equivalent
Returns the equation rewritten as a symbolic function to give negative values when True, positive when False.
EXAMPLES:
sage: from sage.plot.contour_plot import equify
sage: var('x, y')
(x, y)
sage: equify(x^2 < 2)
x |--> x^2 - 2
sage: equify(x^2 > 2)
x |--> -x^2 + 2
sage: equify(x*y > 1)
(x, y) |--> -x*y + 1
sage: equify(y > 0, (x,y))
(x, y) |--> -y
implicit_plot takes a function of two variables,
and plots the curve
over the specified
xrange and yrange as demonstrated below.
implicit_plot(f, (xmin, xmax), (ymin, ymax), ...)
implicit_plot(f, (x, xmin, xmax), (y, ymin, ymax), ...)
INPUT:
The following inputs must all be passed in as named parameters:
EXAMPLES:
A simple circle with a radius of 2. Note that since the input function is an expression, we need to explicitly declare the variables in 3-tuples for the range:
sage: var("x y")
(x, y)
sage: implicit_plot(x^2+y^2-2, (x,-3,3), (y,-3,3)).show(aspect_ratio=1)
I can do the same thing, but using a callable function so I don’t need to explicitly define the variables in the ranges, and filling the inside:
sage: x,y = var('x,y')
sage: f(x,y) = x^2 + y^2 - 2
sage: implicit_plot(f, (-3, 3), (-3, 3),fill=True).show(aspect_ratio=1)
You can also plot an equation:
sage: var("x y")
(x, y)
sage: implicit_plot(x^2+y^2 == 2, (x,-3,3), (y,-3,3)).show(aspect_ratio=1)
We can define a level- approximation of the boundary of the
Mandelbrot set:
sage: def mandel(n):
... c = polygen(CDF, 'c')
... z = 0
... for i in range(n):
... z = z*z + c
... def f(x, y):
... val = z(CDF(x, y))
... return val.norm() - 4
... return f
The first-level approximation is just a circle:
sage: implicit_plot(mandel(1), (-3, 3), (-3, 3)).show(aspect_ratio=1)
A third-level approximation starts to get interesting:
sage: implicit_plot(mandel(3), (-2, 1), (-1.5, 1.5)).show(aspect_ratio=1)
The seventh-level approximation is a degree 64 polynomial, and implicit_plot does a pretty good job on this part of the curve. (plot_points=200 looks even better, but it’s about 16 times slower.)
sage: implicit_plot(mandel(7), (-0.3, 0.05), (-1.15, -0.9),plot_points=50).show(aspect_ratio=1)
Returns the product of all values in vals, with the result nonnegative if any of the values is nonnegative.
EXAMPLES:
sage: from sage.plot.contour_plot import mangle_neg
sage: mangle_neg([-1.2, -0.74, -2.56, -1.01])
-2.29601280000000
sage: mangle_neg([-1.2, 0.0, -2.56])
0.000000000000000
sage: mangle_neg([-1.2, -0.74, -2.56, 1.01])
2.29601280000000
region_plot takes a boolean function of two variables,
and plots the region where f is True over the specified
xrange and yrange as demonstrated below.
region_plot(f, (xmin, xmax), (ymin, ymax), ...)
INPUT:
EXAMPLES:
Here we plot a simple function of two variables:
sage: x,y = var('x,y')
sage: region_plot(cos(x^2+y^2) <= 0, (x, -3, 3), (y, -3, 3))
Here we play with the colors:
sage: region_plot(x^2+y^3 < 2, (x, -2, 2), (y, -2, 2), incol='lightblue', bordercol='gray')
An even more complicated plot:
sage: region_plot(sin(x)*sin(y) >= 1/4, (x,-10,10), (y,-10,10), incol='yellow', bordercol='black', plot_points=100)
A disk centered at the origin:
sage: region_plot(x^2+y^2<1, (x,-1,1), (y,-1,1)).show(aspect_ratio=1)
A plot with more than one condition:
sage: region_plot([x^2+y^2<1, x<y], (x,-2,2), (y,-2,2))
Since it doesn’t look very good, let’s increase plot_points:
sage: region_plot([x^2+y^2<1, x<y], (x,-2,2), (y,-2,2), plot_points=400).show(aspect_ratio=1) #long time
The first quadrant of the unit circle:
sage: region_plot([y>0, x>0, x^2+y^2<1], (-1.1, 1.1), (-1.1, 1.1), plot_points = 400).show(aspect_ratio=1)
Here is another plot:
sage: region_plot(x*(x-1)*(x+1)+y^2<0, (x, -3, 2), (y, -3, 3), incol='lightblue', bordercol='gray', plot_points=50)
If we want to keep only the region where x is positive:
sage: region_plot([x*(x-1)*(x+1)+y^2<0, x>-1], (x, -3, 2), (y, -3, 3), incol='lightblue', bordercol='gray', plot_points=50)
Here we have a cut circle:
sage: region_plot([x^2+y^2<4, x>-1], (x, -2, 2), (y, -2, 2), incol='lightblue', bordercol='gray', plot_points=200).show(aspect_ratio=1) #long time