modelparameters.sympy.plotting package

Subpackages

Submodules

modelparameters.sympy.plotting.experimental_lambdify module

rewrite of lambdify - This stuff is not stable at all.

It is for internal use in the new plotting module. It may (will! see the Q’n’A in the source) be rewritten.

It’s completely self contained. Especially it does not use lambdarepr.

It does not aim to replace the current lambdify. Most importantly it will never ever support anything else than sympy expressions (no Matrices, dictionaries and so on).

class modelparameters.sympy.plotting.experimental_lambdify.Lambdifier(args, expr, print_lambda=False, use_evalf=False, float_wrap_evalf=False, complex_wrap_evalf=False, use_np=False, use_python_math=False, use_python_cmath=False, use_interval=False)[source]

Bases: object

builtin_functions_different = {'Abs': 'abs', 'Max': 'max', 'Min': 'min'}
builtin_not_functions = {'I': '1j'}
cmath_functions_different = {'arg': 'phase', 'ln': 'log'}
cmath_functions_same = ['sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'sinh', 'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'exp', 'log', 'sqrt']
cmath_not_functions = {'E': 'cmath.e', 'pi': 'cmath.pi'}
get_dict_fun()[source]
get_dict_str()[source]
interval_functions_different = {'Max': 'imax', 'Min': 'imin', 'ceiling': 'ceil'}
interval_functions_same = ['sin', 'cos', 'exp', 'tan', 'atan', 'log', 'sqrt', 'cosh', 'sinh', 'tanh', 'floor', 'acos', 'asin', 'acosh', 'asinh', 'atanh', 'Abs', 'And', 'Or']
interval_not_functions = {'E': 'math.e', 'pi': 'math.pi'}
math_functions_different = {'ceiling': 'ceil', 'ln': 'log', 'loggamma': 'lgamma'}
math_functions_same = ['sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2', 'sinh', 'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'exp', 'log', 'erf', 'sqrt', 'floor', 'factorial', 'gamma']
math_not_functions = {'E': 'math.e', 'pi': 'math.pi'}
numpy_functions_different = {'Abs': 'abs', 'Max': 'amax', 'Min': 'amin', 'acos': 'arccos', 'acosh': 'arccosh', 'arg': 'angle', 'asin': 'arcsin', 'asinh': 'arcsinh', 'atan': 'arctan', 'atan2': 'arctan2', 'atanh': 'arctanh', 'ceiling': 'ceil', 'im': 'imag', 'ln': 'log', 're': 'real'}
numpy_functions_same = ['sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh', 'exp', 'log', 'sqrt', 'floor', 'conjugate']
numpy_not_functions = {'E': 'np.e', 'oo': 'np.inf', 'pi': 'np.pi'}
str2tree(exprstr)[source]

Converts an expression string to a tree.

Functions are represented by (‘func_name(’, tree_of_arguments). Other expressions are (head_string, mid_tree, tail_str). Expressions that do not contain functions are directly returned.

Examples

>>> from ..abc import x, y, z
>>> from .. import Integral, sin
>>> from .experimental_lambdify import Lambdifier
>>> str2tree = Lambdifier([x], x).str2tree
>>> str2tree(str(Integral(x, (x, 1, y))))
('', ('Integral(', 'x, (x, 1, y)'), ')')
>>> str2tree(str(x+y))
'x + y'
>>> str2tree(str(x+y*sin(z)+1))
('x + y*', ('sin(', 'z'), ') + 1')
>>> str2tree('sin(y*(y + 1.1) + (sin(y)))')
('', ('sin(', ('y*(y + 1.1) + (', ('sin(', 'y'), '))')), ')')
static sympy_atoms_namespace(expr)[source]

For no real reason this function is separated from sympy_expression_namespace. It can be moved to it.

classmethod sympy_expression_namespace(expr)[source]

Traverses the (func, args) tree of an expression and creates a sympy namespace. All other modules are imported only as a module name. That way the namespace is not poluted and rests quite small. It probably causes much more variable lookups and so it takes more time, but there are no tests on that for the moment.

translate_func(func_name, argtree)[source]

Translate function names and the tree of arguments.

If the function name is not in the dictionaries of dict_tuple_fun then the function is surrounded by a float((…).evalf()).

The use of float is necessary as np.<function>(sympy.Float(..)) raises an error.

translate_str(estr)[source]

Translate substrings of estr using in order the dictionaries in dict_tuple_str.

classmethod tree2str(tree)[source]

Converts a tree to string without translations.

Examples

>>> from ..abc import x, y, z
>>> from .. import Integral, sin
>>> from .experimental_lambdify import Lambdifier
>>> str2tree = Lambdifier([x], x).str2tree
>>> tree2str = Lambdifier([x], x).tree2str
>>> tree2str(str2tree(str(x+y*sin(z)+1)))
'x + y*sin(z) + 1'
tree2str_translate(tree)[source]

Converts a tree to string with translations.

Function names are translated by translate_func. Other strings are translated by translate_str.

modelparameters.sympy.plotting.experimental_lambdify.experimental_lambdify(*args, **kwargs)[source]
class modelparameters.sympy.plotting.experimental_lambdify.lambdify(args, expr)[source]

Bases: object

Returns the lambdified function.

This function uses experimental_lambdify to create a lambdified expression. It uses cmath to lambdify the expression. If the function is not implemented in python cmath, python cmath calls evalf on those functions.

class modelparameters.sympy.plotting.experimental_lambdify.vectorized_lambdify(args, expr)[source]

Bases: object

Return a sufficiently smart, vectorized and lambdified function.

Returns only reals.

This function uses experimental_lambdify to created a lambdified expression ready to be used with numpy. Many of the functions in sympy are not implemented in numpy so in some cases we resort to python cmath or even to evalf.

The following translations are tried:

only numpy complex - on errors raised by sympy trying to work with ndarray:

only python cmath and then vectorize complex128

When using python cmath there is no need for evalf or float/complex because python cmath calls those.

This function never tries to mix numpy directly with evalf because numpy does not understand sympy Float. If this is needed one can use the float_wrap_evalf/complex_wrap_evalf options of experimental_lambdify or better one can be explicit about the dtypes that numpy works with. Check numpy bug http://projects.scipy.org/numpy/ticket/1013 to know what types of errors to expect.

modelparameters.sympy.plotting.plot module

Plotting module for Sympy.

A plot is represented by the Plot class that contains a reference to the backend and a list of the data series to be plotted. The data series are instances of classes meant to simplify getting points and meshes from sympy expressions. plot_backends is a dictionary with all the backends.

This module gives only the essential. For all the fancy stuff use directly the backend. You can get the backend wrapper for every plot from the _backend attribute. Moreover the data series classes have various useful methods like get_points, get_segments, get_meshes, etc, that may be useful if you wish to use another plotting library.

Especially if you need publication ready graphs and this module is not enough for you - just get the _backend attribute and add whatever you want directly to it. In the case of matplotlib (the common way to graph data in python) just copy _backend.fig which is the figure and _backend.ax which is the axis and work on them as you would on any other matplotlib object.

Simplicity of code takes much greater importance than performance. Don’t use it if you care at all about performance. A new backend instance is initialized every time you call show() and the old one is left to the garbage collector.

class modelparameters.sympy.plotting.plot.BaseBackend(parent)[source]

Bases: object

class modelparameters.sympy.plotting.plot.BaseSeries[source]

Bases: object

Base class for the data objects containing stuff to be plotted.

The backend should check if it supports the data series that it’s given. (eg TextBackend supports only LineOver1DRange). It’s the backend responsibility to know how to use the class of data series that it’s given.

Some data series classes are grouped (using a class attribute like is_2Dline) according to the api they present (based only on convention). The backend is not obliged to use that api (eg. The LineOver1DRange belongs to the is_2Dline group and presents the get_points method, but the TextBackend does not use the get_points method).

is_2Dline = False
property is_3D
is_3Dline = False
is_3Dsurface = False
is_contour = False
is_implicit = False
property is_line
is_parametric = False
class modelparameters.sympy.plotting.plot.ContourSeries(expr, var_start_end_x, var_start_end_y)[source]

Bases: BaseSeries

Representation for a contour plot.

get_meshes()[source]
is_contour = True
class modelparameters.sympy.plotting.plot.DefaultBackend(parent)[source]

Bases: BaseBackend

class modelparameters.sympy.plotting.plot.Line2DBaseSeries[source]

Bases: BaseSeries

A base class for 2D lines.

  • adding the label, steps and only_integers options

  • making is_2Dline true

  • defining get_segments and get_color_array

get_color_array()[source]
get_segments()[source]
is_2Dline = True
class modelparameters.sympy.plotting.plot.Line3DBaseSeries[source]

Bases: Line2DBaseSeries

A base class for 3D lines.

Most of the stuff is derived from Line2DBaseSeries.

is_2Dline = False
is_3Dline = True
class modelparameters.sympy.plotting.plot.LineOver1DRangeSeries(expr, var_start_end, **kwargs)[source]

Bases: Line2DBaseSeries

Representation for a line consisting of a SymPy expression over a range.

get_points()[source]
get_segments()[source]

Adaptively gets segments for plotting.

The adaptive sampling is done by recursively checking if three points are almost collinear. If they are not collinear, then more points are added between those points.

References

[1] Adaptive polygonal approximation of parametric curves,

Luiz Henrique de Figueiredo.

class modelparameters.sympy.plotting.plot.List2DSeries(list_x, list_y)[source]

Bases: Line2DBaseSeries

Representation for a line consisting of list of points.

get_points()[source]
class modelparameters.sympy.plotting.plot.MatplotlibBackend(parent)[source]

Bases: BaseBackend

close()[source]
process_series()[source]
save(path)[source]
show()[source]
class modelparameters.sympy.plotting.plot.Parametric2DLineSeries(expr_x, expr_y, var_start_end, **kwargs)[source]

Bases: Line2DBaseSeries

Representation for a line consisting of two parametric sympy expressions over a range.

get_parameter_points()[source]
get_points()[source]
get_segments()[source]

Adaptively gets segments for plotting.

The adaptive sampling is done by recursively checking if three points are almost collinear. If they are not collinear, then more points are added between those points.

References

[1] Adaptive polygonal approximation of parametric curves,

Luiz Henrique de Figueiredo.

is_parametric = True
class modelparameters.sympy.plotting.plot.Parametric3DLineSeries(expr_x, expr_y, expr_z, var_start_end, **kwargs)[source]

Bases: Line3DBaseSeries

Representation for a 3D line consisting of two parametric sympy expressions and a range.

get_parameter_points()[source]
get_points()[source]
class modelparameters.sympy.plotting.plot.ParametricSurfaceSeries(expr_x, expr_y, expr_z, var_start_end_u, var_start_end_v, **kwargs)[source]

Bases: SurfaceBaseSeries

Representation for a 3D surface consisting of three parametric sympy expressions and a range.

get_meshes()[source]
get_parameter_meshes()[source]
is_parametric = True
class modelparameters.sympy.plotting.plot.Plot(*args, **kwargs)[source]

Bases: object

The central class of the plotting module.

For interactive work the function plot is better suited.

This class permits the plotting of sympy expressions using numerous backends (matplotlib, textplot, the old pyglet module for sympy, Google charts api, etc).

The figure can contain an arbitrary number of plots of sympy expressions, lists of coordinates of points, etc. Plot has a private attribute _series that contains all data series to be plotted (expressions for lines or surfaces, lists of points, etc (all subclasses of BaseSeries)). Those data series are instances of classes not imported by from .. import *.

The customization of the figure is on two levels. Global options that concern the figure as a whole (eg title, xlabel, scale, etc) and per-data series options (eg name) and aesthetics (eg. color, point shape, line type, etc.).

The difference between options and aesthetics is that an aesthetic can be a function of the coordinates (or parameters in a parametric plot). The supported values for an aesthetic are: - None (the backend uses default values) - a constant - a function of one variable (the first coordinate or parameter) - a function of two variables (the first and second coordinate or parameters) - a function of three variables (only in nonparametric 3D plots) Their implementation depends on the backend so they may not work in some backends.

If the plot is parametric and the arity of the aesthetic function permits it the aesthetic is calculated over parameters and not over coordinates. If the arity does not permit calculation over parameters the calculation is done over coordinates.

Only cartesian coordinates are supported for the moment, but you can use the parametric plots to plot in polar, spherical and cylindrical coordinates.

The arguments for the constructor Plot must be subclasses of BaseSeries.

Any global option can be specified as a keyword argument.

The global options for a figure are:

  • title : str

  • xlabel : str

  • ylabel : str

  • legend : bool

  • xscale : {‘linear’, ‘log’}

  • yscale : {‘linear’, ‘log’}

  • axis : bool

  • axis_center : tuple of two floats or {‘center’, ‘auto’}

  • xlim : tuple of two floats

  • ylim : tuple of two floats

  • aspect_ratio : tuple of two floats or {‘auto’}

  • autoscale : bool

  • margin : float in [0, 1]

The per data series options and aesthetics are: There are none in the base series. See below for options for subclasses.

Some data series support additional aesthetics or options:

ListSeries, LineOver1DRangeSeries, Parametric2DLineSeries, Parametric3DLineSeries support the following:

Aesthetics:

  • line_color : function which returns a float.

options:

  • label : str

  • steps : bool

  • integers_only : bool

SurfaceOver2DRangeSeries, ParametricSurfaceSeries support the following:

aesthetics:

  • surface_color : function which returns a float.

append(arg)[source]

Adds an element from a plot’s series to an existing plot.

Examples

Consider two Plot objects, p1 and p2. To add the second plot’s first series object to the first, use the append method, like so:

>>> from .. import symbols
>>> from ..plotting import plot
>>> x = symbols('x')
>>> p1 = plot(x*x)
>>> p2 = plot(x)
>>> p1.append(p2[0])
>>> p1
Plot object containing:
[0]: cartesian line: x**2 for x over (-10.0, 10.0)
[1]: cartesian line: x for x over (-10.0, 10.0)

See also

extend

extend(arg)[source]

Adds all series from another plot.

Examples

Consider two Plot objects, p1 and p2. To add the second plot to the first, use the extend method, like so:

>>> from .. import symbols
>>> from ..plotting import plot
>>> x = symbols('x')
>>> p1 = plot(x*x)
>>> p2 = plot(x)
>>> p1.extend(p2)
>>> p1
Plot object containing:
[0]: cartesian line: x**2 for x over (-10.0, 10.0)
[1]: cartesian line: x for x over (-10.0, 10.0)
save(path)[source]
show()[source]
class modelparameters.sympy.plotting.plot.SurfaceBaseSeries[source]

Bases: BaseSeries

A base class for 3D surfaces.

get_color_array()[source]
is_3Dsurface = True
class modelparameters.sympy.plotting.plot.SurfaceOver2DRangeSeries(expr, var_start_end_x, var_start_end_y, **kwargs)[source]

Bases: SurfaceBaseSeries

Representation for a 3D surface consisting of a sympy expression and 2D range.

get_meshes()[source]
class modelparameters.sympy.plotting.plot.TextBackend(parent)[source]

Bases: BaseBackend

close()[source]
show()[source]
modelparameters.sympy.plotting.plot.centers_of_faces(array)[source]
modelparameters.sympy.plotting.plot.centers_of_segments(array)[source]
modelparameters.sympy.plotting.plot.check_arguments(args, expr_len, nb_of_free_symbols)[source]

Checks the arguments and converts into tuples of the form (exprs, ranges)

Examples

>>> from .. import plot, cos, sin, symbols
>>> from .plot import check_arguments
>>> x = symbols('x')
>>> check_arguments([cos(x), sin(x)], 2, 1)
    [(cos(x), sin(x), (x, -10, 10))]
>>> check_arguments([x, x**2], 1, 1)
    [(x, (x, -10, 10)), (x**2, (x, -10, 10))]
modelparameters.sympy.plotting.plot.flat(x, y, z, eps=0.001)[source]

Checks whether three points are almost collinear

modelparameters.sympy.plotting.plot.plot(*args, **kwargs)[source]

Plots a function of a single variable and returns an instance of the Plot class (also, see the description of the show keyword argument below).

The plotting uses an adaptive algorithm which samples recursively to accurately plot the plot. The adaptive algorithm uses a random point near the midpoint of two points that has to be further sampled. Hence the same plots can appear slightly different.

Usage

Single Plot

plot(expr, range, **kwargs)

If the range is not specified, then a default range of (-10, 10) is used.

Multiple plots with same range.

plot(expr1, expr2, ..., range, **kwargs)

If the range is not specified, then a default range of (-10, 10) is used.

Multiple plots with different ranges.

plot((expr1, range), (expr2, range), ..., **kwargs)

Range has to be specified for every expression.

Default range may change in the future if a more advanced default range detection algorithm is implemented.

param expr:

type expr:

Expression representing the function of single variable

param range:

type range:

(x, 0, 5), A 3-tuple denoting the range of the free variable.

keyword Arguments for plot function:

keyword show:

kwtype show:

Boolean. The default value is set to True. Set show to

keyword False and the function will not display the plot. The returned:

keyword instance of the Plot class can then be used to save or display:

keyword the plot by calling the save() and show() methods:

keyword respectively.:

keyword Arguments for LineOver1DRangeSeries class:

keyword adaptive:

kwtype adaptive:

Boolean. The default value is set to True. Set adaptive to False and

keyword specify nb_of_points if uniform sampling is required.:

keyword depth:

kwtype depth:

int Recursion depth of the adaptive algorithm. A depth of value n

keyword samples a maximum of 2^{n} points.:

keyword nb_of_points:

kwtype nb_of_points:

int. Used when the adaptive is set to False. The function

keyword is uniformly sampled at nb_of_points number of points.:

keyword Aesthetics options:

keyword line_color:

kwtype line_color:

float. Specifies the color for the plot.

keyword See Plot to see how to set color for the plots.:

keyword If there are multiple plots, then the same series series are applied to:

keyword all the plots. If you want to set these options separately, you can index:

keyword the Plot object returned and set it.:

keyword Arguments for Plot class:

keyword title:

kwtype title:

str. Title of the plot. It is set to the latex representation of

keyword the expression, if the plot has only one expression.:

keyword xlabel:

kwtype xlabel:

str. Label for the x-axis.

keyword ylabel:

kwtype ylabel:

str. Label for the y-axis.

keyword xscale:

kwtype xscale:

{‘linear’, ‘log’} Sets the scaling of the x-axis.

keyword yscale:

kwtype yscale:

{‘linear’, ‘log’} Sets the scaling if the y-axis.

keyword axis_center:

kwtype axis_center:

tuple of two floats denoting the coordinates of the center or

keyword {‘center’, ‘auto’}:

keyword xlim:

kwtype xlim:

tuple of two floats, denoting the x-axis limits.

keyword ylim:

kwtype ylim:

tuple of two floats, denoting the y-axis limits.

Examples

>>> from .. import symbols
>>> from ..plotting import plot
>>> x = symbols('x')

Single Plot

>>> plot(x**2, (x, -5, 5))
Plot object containing:
[0]: cartesian line: x**2 for x over (-5.0, 5.0)

Multiple plots with single range.

>>> plot(x, x**2, x**3, (x, -5, 5))
Plot object containing:
[0]: cartesian line: x for x over (-5.0, 5.0)
[1]: cartesian line: x**2 for x over (-5.0, 5.0)
[2]: cartesian line: x**3 for x over (-5.0, 5.0)

Multiple plots with different ranges.

>>> plot((x**2, (x, -6, 6)), (x, (x, -5, 5)))
Plot object containing:
[0]: cartesian line: x**2 for x over (-6.0, 6.0)
[1]: cartesian line: x for x over (-5.0, 5.0)

No adaptive sampling.

>>> plot(x**2, adaptive=False, nb_of_points=400)
Plot object containing:
[0]: cartesian line: x**2 for x over (-10.0, 10.0)

See also

Plot, LineOver1DRangeSeries.

modelparameters.sympy.plotting.plot.plot3d(*args, **kwargs)[source]

Plots a 3D surface plot.

Usage

Single plot

plot3d(expr, range_x, range_y, **kwargs)

If the ranges are not specified, then a default range of (-10, 10) is used.

Multiple plot with the same range.

plot3d(expr1, expr2, range_x, range_y, **kwargs)

If the ranges are not specified, then a default range of (-10, 10) is used.

Multiple plots with different ranges.

plot3d((expr1, range_x, range_y), (expr2, range_x, range_y), ..., **kwargs)

Ranges have to be specified for every expression.

Default range may change in the future if a more advanced default range detection algorithm is implemented.

param expr:

type expr:

Expression representing the function along x.

param range_x:

type range_x:

(x, 0, 5), A 3-tuple denoting the range of the x

param variable.:

param range_y:

variable.

type range_y:

(y, 0, 5), A 3-tuple denoting the range of the y

keyword Arguments for SurfaceOver2DRangeSeries class:

keyword nb_of_points_x:

kwtype nb_of_points_x:

int. The x range is sampled uniformly at

keyword nb_of_points_x of points.:

keyword nb_of_points_y:

kwtype nb_of_points_y:

int. The y range is sampled uniformly at

keyword nb_of_points_y of points.:

keyword Aesthetics:

keyword surface_color:

kwtype surface_color:

Function which returns a float. Specifies the color for

keyword the surface of the plot. See sympy.plotting.Plot for more details.:

keyword If there are multiple plots, then the same series arguments are applied to:

keyword all the plots. If you want to set these options separately, you can index:

keyword the returned Plot object and set it.:

keyword Arguments for Plot class:

keyword title:

kwtype title:

str. Title of the plot.

Examples

>>> from .. import symbols
>>> from ..plotting import plot3d
>>> x, y = symbols('x y')

Single plot

>>> plot3d(x*y, (x, -5, 5), (y, -5, 5))
Plot object containing:
[0]: cartesian surface: x*y for x over (-5.0, 5.0) and y over (-5.0, 5.0)

Multiple plots with same range

>>> plot3d(x*y, -x*y, (x, -5, 5), (y, -5, 5))
Plot object containing:
[0]: cartesian surface: x*y for x over (-5.0, 5.0) and y over (-5.0, 5.0)
[1]: cartesian surface: -x*y for x over (-5.0, 5.0) and y over (-5.0, 5.0)

Multiple plots with different ranges.

>>> plot3d((x**2 + y**2, (x, -5, 5), (y, -5, 5)),
...     (x*y, (x, -3, 3), (y, -3, 3)))
Plot object containing:
[0]: cartesian surface: x**2 + y**2 for x over (-5.0, 5.0) and y over (-5.0, 5.0)
[1]: cartesian surface: x*y for x over (-3.0, 3.0) and y over (-3.0, 3.0)
modelparameters.sympy.plotting.plot.plot3d_parametric_line(*args, **kwargs)[source]

Plots a 3D parametric line plot.

Usage

Single plot:

plot3d_parametric_line(expr_x, expr_y, expr_z, range, **kwargs)

If the range is not specified, then a default range of (-10, 10) is used.

Multiple plots.

plot3d_parametric_line((expr_x, expr_y, expr_z, range), ..., **kwargs)

Ranges have to be specified for every expression.

Default range may change in the future if a more advanced default range detection algorithm is implemented.

param expr_x:

type expr_x:

Expression representing the function along x.

param expr_y:

type expr_y:

Expression representing the function along y.

param expr_z:

type expr_z:

Expression representing the function along z.

param range:

type range:

(u, 0, 5), A 3-tuple denoting the range of the parameter

param variable.:

keyword Arguments for Parametric3DLineSeries class.:

keyword nb_of_points:

kwtype nb_of_points:

The range is uniformly sampled at nb_of_points

keyword number of points.:

keyword Aesthetics:

keyword line_color:

kwtype line_color:

function which returns a float. Specifies the color for the

keyword plot. See sympy.plotting.Plot for more details.:

keyword If there are multiple plots, then the same series arguments are applied to:

keyword all the plots. If you want to set these options separately, you can index:

keyword the returned Plot object and set it.:

keyword Arguments for Plot class.:

keyword title:

kwtype title:

str. Title of the plot.

Examples

>>> from .. import symbols, cos, sin
>>> from ..plotting import plot3d_parametric_line
>>> u = symbols('u')

Single plot.

>>> plot3d_parametric_line(cos(u), sin(u), u, (u, -5, 5))
Plot object containing:
[0]: 3D parametric cartesian line: (cos(u), sin(u), u) for u over (-5.0, 5.0)

Multiple plots.

>>> plot3d_parametric_line((cos(u), sin(u), u, (u, -5, 5)),
...     (sin(u), u**2, u, (u, -5, 5)))
Plot object containing:
[0]: 3D parametric cartesian line: (cos(u), sin(u), u) for u over (-5.0, 5.0)
[1]: 3D parametric cartesian line: (sin(u), u**2, u) for u over (-5.0, 5.0)
modelparameters.sympy.plotting.plot.plot3d_parametric_surface(*args, **kwargs)[source]

Plots a 3D parametric surface plot.

Usage

Single plot.

plot3d_parametric_surface(expr_x, expr_y, expr_z, range_u, range_v, **kwargs)

If the ranges is not specified, then a default range of (-10, 10) is used.

Multiple plots.

plot3d_parametric_surface((expr_x, expr_y, expr_z, range_u, range_v), ..., **kwargs)

Ranges have to be specified for every expression.

Default range may change in the future if a more advanced default range detection algorithm is implemented.

param expr_x:

type expr_x:

Expression representing the function along x.

param expr_y:

type expr_y:

Expression representing the function along y.

param expr_z:

type expr_z:

Expression representing the function along z.

param range_u:

type range_u:

(u, 0, 5), A 3-tuple denoting the range of the u

param variable.:

param range_v:

type range_v:

(v, 0, 5), A 3-tuple denoting the range of the v

param variable.:

keyword Arguments for ParametricSurfaceSeries class:

keyword nb_of_points_u:

kwtype nb_of_points_u:

int. The u range is sampled uniformly at

keyword nb_of_points_v of points:

keyword nb_of_points_y:

kwtype nb_of_points_y:

int. The v range is sampled uniformly at

keyword nb_of_points_y of points:

keyword Aesthetics:

keyword surface_color:

kwtype surface_color:

Function which returns a float. Specifies the color for

keyword the surface of the plot. See sympy.plotting.Plot for more details.:

keyword If there are multiple plots, then the same series arguments are applied for:

keyword all the plots. If you want to set these options separately, you can index:

keyword the returned Plot object and set it.:

Arguments for Plot class:

title : str. Title of the plot.

Examples

>>> from .. import symbols, cos, sin
>>> from ..plotting import plot3d_parametric_surface
>>> u, v = symbols('u v')

Single plot.

>>> plot3d_parametric_surface(cos(u + v), sin(u - v), u - v,
...     (u, -5, 5), (v, -5, 5))
Plot object containing:
[0]: parametric cartesian surface: (cos(u + v), sin(u - v), u - v) for u over (-5.0, 5.0) and v over (-5.0, 5.0)
modelparameters.sympy.plotting.plot.plot_parametric(*args, **kwargs)[source]

Plots a 2D parametric plot.

The plotting uses an adaptive algorithm which samples recursively to accurately plot the plot. The adaptive algorithm uses a random point near the midpoint of two points that has to be further sampled. Hence the same plots can appear slightly different.

Usage

Single plot.

plot_parametric(expr_x, expr_y, range, **kwargs)

If the range is not specified, then a default range of (-10, 10) is used.

Multiple plots with same range.

plot_parametric((expr1_x, expr1_y), (expr2_x, expr2_y), range, **kwargs)

If the range is not specified, then a default range of (-10, 10) is used.

Multiple plots with different ranges.

plot_parametric((expr_x, expr_y, range), ..., **kwargs)

Range has to be specified for every expression.

Default range may change in the future if a more advanced default range detection algorithm is implemented.

param expr_x:

type expr_x:

Expression representing the function along x.

param expr_y:

type expr_y:

Expression representing the function along y.

param range:

type range:

(u, 0, 5), A 3-tuple denoting the range of the parameter

param variable.:

keyword Arguments for Parametric2DLineSeries class:

keyword adaptive:

kwtype adaptive:

Boolean. The default value is set to True. Set adaptive to

keyword False and specify nb_of_points if uniform sampling is required.:

keyword depth:

kwtype depth:

int Recursion depth of the adaptive algorithm. A depth of

keyword value n samples a maximum of 2^{n} points.:

keyword nb_of_points:

kwtype nb_of_points:

int. Used when the adaptive is set to False. The

keyword function is uniformly sampled at nb_of_points number of points.:

keyword Aesthetics:

keyword ———-:

keyword line_color:

kwtype line_color:

function which returns a float. Specifies the color for the

keyword plot. See sympy.plotting.Plot for more details.:

keyword If there are multiple plots, then the same Series arguments are applied to:

keyword all the plots. If you want to set these options separately, you can index:

keyword the returned Plot object and set it.:

keyword Arguments for Plot class:

keyword xlabel:

kwtype xlabel:

str. Label for the x-axis.

keyword ylabel:

kwtype ylabel:

str. Label for the y-axis.

keyword xscale:

kwtype xscale:

{‘linear’, ‘log’} Sets the scaling of the x-axis.

keyword yscale:

kwtype yscale:

{‘linear’, ‘log’} Sets the scaling if the y-axis.

keyword axis_center:

kwtype axis_center:

tuple of two floats denoting the coordinates of the center

keyword or {‘center’, ‘auto’}:

keyword xlim:

kwtype xlim:

tuple of two floats, denoting the x-axis limits.

keyword ylim:

kwtype ylim:

tuple of two floats, denoting the y-axis limits.

Examples

>>> from .. import symbols, cos, sin
>>> from ..plotting import plot_parametric
>>> u = symbols('u')

Single Parametric plot

>>> plot_parametric(cos(u), sin(u), (u, -5, 5))
Plot object containing:
[0]: parametric cartesian line: (cos(u), sin(u)) for u over (-5.0, 5.0)

Multiple parametric plot with single range.

>>> plot_parametric((cos(u), sin(u)), (u, cos(u)))
Plot object containing:
[0]: parametric cartesian line: (cos(u), sin(u)) for u over (-10.0, 10.0)
[1]: parametric cartesian line: (u, cos(u)) for u over (-10.0, 10.0)

Multiple parametric plots.

>>> plot_parametric((cos(u), sin(u), (u, -5, 5)),
...     (cos(u), u, (u, -5, 5)))
Plot object containing:
[0]: parametric cartesian line: (cos(u), sin(u)) for u over (-5.0, 5.0)
[1]: parametric cartesian line: (cos(u), u) for u over (-5.0, 5.0)
modelparameters.sympy.plotting.plot.unset_show()[source]

modelparameters.sympy.plotting.plot_implicit module

Implicit plotting module for SymPy

The module implements a data series called ImplicitSeries which is used by Plot class to plot implicit plots for different backends. The module, by default, implements plotting using interval arithmetic. It switches to a fall back algorithm if the expression cannot be plotted using interval arithmetic. It is also possible to specify to use the fall back algorithm for all plots.

Boolean combinations of expressions cannot be plotted by the fall back algorithm.

See also

sympy.plotting.plot

References

  • Jeffrey Allen Tupper. Reliable Two-Dimensional Graphing Methods for

Mathematical Formulae with Two Free Variables.

  • Jeffrey Allen Tupper. Graphing Equations with Generalized Interval

Arithmetic. Master’s thesis. University of Toronto, 1996

class modelparameters.sympy.plotting.plot_implicit.ImplicitSeries(expr, var_start_end_x, var_start_end_y, has_equality, use_interval_math, depth, nb_of_points, line_color)[source]

Bases: BaseSeries

Representation for Implicit plot

get_raster()[source]
is_implicit = True
modelparameters.sympy.plotting.plot_implicit.plot_implicit(expr, x_var=None, y_var=None, **kwargs)[source]

A plot function to plot implicit equations / inequalities.

Parameters:
  • expr (-) –

  • (optional) (- y_var) – and range as (symbol, xmin, xmax)

  • (optional) – and range as (symbol, ymin, ymax)

  • the (If neither x_var nor y_var are given then the free symbols in) –

  • sorted. (expression will be assigned in the order they are) –

  • used (The following keyword arguments can also be) –

  • be (- adaptive. Boolean. The default value is set to True. It has to) – set to False if you want to use a mesh grid.

  • grid. (- depth integer. The depth of recursion for adaptive mesh) – Default value is 0. Takes value in the range (0, 4).

  • not (- points integer. The number of points if adaptive mesh grid is) – used. Default value is 200.

  • plot. (- title string .The title for the) –

  • x-axis (- xlabel string. The label for the) –

  • y-axis (- ylabel string. The label for the) –

  • options (Aesthetics) –

  • line_color (-) – See Plot to see how to set color for the plots.

  • plot_implicit

  • default (by) –

  • If (uses interval arithmetic to plot functions.) –

  • arithmetic (interval) –

  • to (it defaults) –

  • By (a generating a contour using a mesh grid of fixed number of points.) –

  • False (setting adaptive to) –

  • mesh (you can force plot_implicit to use the) –

  • using (grid. The mesh grid method can be effective when adaptive plotting) –

  • arithmetic

  • width. (fails to plot with small line) –

Examples

Plot expressions:

>>> from .. import plot_implicit, cos, sin, symbols, Eq, And
>>> x, y = symbols('x y')

Without any ranges for the symbols in the expression

>>> p1 = plot_implicit(Eq(x**2 + y**2, 5))

With the range for the symbols

>>> p2 = plot_implicit(Eq(x**2 + y**2, 3),
...         (x, -3, 3), (y, -3, 3))

With depth of recursion as argument.

>>> p3 = plot_implicit(Eq(x**2 + y**2, 5),
...         (x, -4, 4), (y, -4, 4), depth = 2)

Using mesh grid and not using adaptive meshing.

>>> p4 = plot_implicit(Eq(x**2 + y**2, 5),
...         (x, -5, 5), (y, -2, 2), adaptive=False)

Using mesh grid with number of points as input.

>>> p5 = plot_implicit(Eq(x**2 + y**2, 5),
...         (x, -5, 5), (y, -2, 2),
...         adaptive=False, points=400)

Plotting regions.

>>> p6 = plot_implicit(y > x**2)

Plotting Using boolean conjunctions.

>>> p7 = plot_implicit(And(y > x, y > -x))

When plotting an expression with a single variable (y - 1, for example), specify the x or the y variable explicitly:

>>> p8 = plot_implicit(y - 1, y_var=y)
>>> p9 = plot_implicit(x - 1, x_var=x)

modelparameters.sympy.plotting.textplot module

modelparameters.sympy.plotting.textplot.textplot(expr, a, b, W=55, H=18)[source]

Print a crude ASCII art plot of the SymPy expression ‘expr’ (which should contain a single symbol, e.g. x or something else) over the interval [a, b].

Examples

textplot(sin(t)*t, 0, 15)

Module contents