 Sharedsupport / 2014-11-12-bugs.sagewsOpen in CoCalc
Authors: Harald Schilly, ℏal Snyder, William A. Stein
Description: Examples for support purposes.
var('x','y')
implicit_plot??(x*y-1, (x,0,2), (y,0,2), axes_labels=["$x$", "$y$"], color='red', legend_label="abc")

(x, y) implicit_plot??

   File: /usr/local/sage/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/misc/decorators.py
Source:
@options(plot_points=150, contours=(0,0), fill=False, cmap=["blue"])
def implicit_plot(f, xrange, yrange, **options):
r"""
implicit_plot takes a function of two variables, f(x,y)
and plots the curve f(x,y) = 0 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:

- f -- a function of two variables or equation in two variables

- (xmin, xmax) -- 2-tuple, the range of x values or (x,xmin,xmax)

- (ymin, ymax) -- 2-tuple, the range of y values or (y,ymin,ymax)

The following inputs must all be passed in as named parameters:

- plot_points -- integer (default: 150); number of points to plot
in each direction of the grid

- fill -- boolean (default: False); if True, fill the region
f(x,y) < 0.

- linewidth -- integer (default: None), if a single integer all levels
will be of the width given, otherwise the levels will be plotted with the
widths in the order given.

- linestyle -- string (default: None), the style of the line to be
plotted, one of: "solid", "dashed", "dashdot" or
"dotted", respectively "-", "--", "-.", or ":".

- color -- string (default: blue), the color of the plot. Colors are
defined in :mod:sage.plot.colors; try colors? to see them all.

- legend_label -- the label for this item in the legend

- base - (default: 10) the base of the logarithm if
a logarithmic scale is set. This must be greater than 1. The base
can be also given as a list or tuple (basex, basey).
basex sets the base of the logarithm along the horizontal
axis and basey sets the base along the vertical axis.

- scale -- (default: "linear") string. The scale of the axes.
Possible values are "linear", "loglog", "semilogx",
"semilogy".

The scale can be also be given as single argument that is a list
or tuple (scale, base) or (scale, basex, basey).

The "loglog" scale sets both the horizontal and vertical axes to
logarithmic scale. The "semilogx" scale sets the horizontal axis
to logarithmic scale. The "semilogy" scale sets the vertical axis
to logarithmic scale. The "linear" scale is the default value
when :class:~sage.plot.graphics.Graphics is initialized.

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

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: f(x,y) = x^2 + y^2 - 2
sage: implicit_plot(f, (-3, 3), (-3, 3),fill=True)

The same circle but with a different line width::

sage: implicit_plot(f, (-3,3), (-3,3), linewidth=6)

And again the same circle but this time with a dashdot border::

sage: implicit_plot(f, (-3,3), (-3,3), linestyle='dashdot')

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

You can even change the color of the plot::

sage: implicit_plot(x^2+y^2 == 2, (x,-3,3), (y,-3,3), color="red")

Here is a beautiful (and long) example which also tests that all
colors work with this::

sage: G = Graphics()
sage: counter = 0
sage: for col in colors.keys(): # long time
...       G += implicit_plot(x^2+y^2==1+counter*.1, (x,-4,4),(y,-4,4),color=col)
...       counter += 1
sage: G.show(frame=False)

We can define a level-n 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))

A third-level approximation starts to get interesting::

sage: implicit_plot(mandel(3), (-2, 1), (-1.5, 1.5))

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 takes over a second.)

::

sage: implicit_plot(mandel(7), (-0.3, 0.05), (-1.15, -0.9),plot_points=50)

When making a filled implicit plot using a python function rather than a
symbolic expression the user should increase the number of plot points to
avoid artifacts::

sage: implicit_plot(lambda x,y: x^2+y^2-2, (x,-3,3), (y,-3,3), fill=True, plot_points=500) # long time

An example of an implicit plot on 'loglog' scale::

sage: implicit_plot(x^2+y^2 == 200, (x,1,200), (y,1,200), scale='loglog')

TESTS::

sage: f(x,y) = x^2 + y^2 - 2
sage: implicit_plot(f, (-3, 3), (-3, 3),fill=5)
Traceback (most recent call last):
...
ValueError: fill=5 is not supported
"""
from sage.symbolic.expression import is_SymbolicEquation
if is_SymbolicEquation(f):
if f.operator() != operator.eq:
raise ValueError("input to implicit plot must be function or equation")
f = f.lhs() - f.rhs()
linewidths = options.pop('linewidth', None)
linestyles = options.pop('linestyle', None)

if 'color' in options:
options['cmap']=[options.pop('color', None)]

if options['fill'] is True:
options.pop('fill')
options.pop('contours',None)
options.pop('cmap',None)
from sage.symbolic.expression import is_Expression
if not is_Expression(f):
return region_plot(lambda x,y: f(x,y)<0, xrange, yrange,
borderwidth=linewidths, borderstyle=linestyles,
**options)
else:
return region_plot(f<0, xrange, yrange, borderwidth=linewidths,
borderstyle=linestyles, **options)
elif options['fill'] is False:
return contour_plot(f, xrange, yrange, linewidths=linewidths,
linestyles=linestyles, **options)
else:
raise ValueError("fill=%s is not supported" % options['fill'])