Skip to content

Commit

Permalink
Removed the plot function
Browse files Browse the repository at this point in the history
1) The set_item cannot take plot arguments as input
2) Overloaded Series Factory is deleted.
  • Loading branch information
catchmrbharath committed Aug 23, 2012
1 parent 1631162 commit 866f93f
Showing 1 changed file with 0 additions and 231 deletions.
231 changes: 0 additions & 231 deletions sympy/plotting/plot.py
Expand Up @@ -200,12 +200,8 @@ def __getitem__(self, index):
return self._series[index]

def __setitem__(self, index, *args):
# XXX for ease of use here we permit also arguments for plot()
if len(args)==1 and isinstance(args[0], BaseSeries):
self._series[index] = args
else:
p = plot(*args, **{'show':False})
self.extend(p)

def __delitem__(self, index):
del self._series[index]
Expand All @@ -225,184 +221,6 @@ def extend(self, arg):
self._series.extend(arg)


def plot(*args, **kwargs):
"""A plot function for interactive use.
It implements many heuristics in order to guess what the user wants on
incomplete input.
There is also the 'show' argument that defaults to True (immediately
showing the plot).
The input arguments can be:
- lists with coordinates for ploting a line in 2D or 3D
- the expressions and variable lists with ranges in order to plot any of
the following: 2d line, 2d parametric line, 3d parametric line,
surface, parametric surface
- if the variable lists do not provide ranges a default range is used
- if the variables are not provided, the free variables are
automatically supplied in the order they are sorted (e.g. x, y, z)
- if neither variables nor ranges are provided, both are guessed
- if multiple expressions are provided in a list all of them are
plotted
- an instance of BaseSeries() subclass
- another Plot() instance
- tuples containing any of the above mentioned options, for plotting them
together
In the case of 2D line and parametric plots, an adaptive sampling algorithm
is used. The adaptive sampling algorithm recursively selects a random point
in between two previously sampled points, which might lead to slightly
different plots being rendered each time.
Examples:
---------
Plot expressions:
>>> from sympy import plot, cos, sin, symbols
>>> x,y,u,v = symbols('x y u v')
>>> p1 = plot(x**2, show=False) # with default [-10,+10] range
>>> p2 = plot(x**2, (0, 5), show=False) # it finds the free variable itself
>>> p3 = plot(x**2, (x, 0, 5), show=False) # fully explicit
Fully implicit examples (finding the free variable and using default
range). For the explicit versions just add the tuples with ranges:
>>> p4 = plot(x**2, show=False) # cartesian line
>>> p5 = plot(cos(u), sin(u), show=False) # parametric line
>>> p6 = plot(cos(u), sin(u), u, show=False) # parametric line in 3d
>>> p7 = plot(x**2 + y**2, show=False) # cartesian surface
>>> p8 = plot(u, v, u+v, show=False) # parametric surface
Multiple plots per figure:
>>> p9 = plot((x**2, ), (cos(u), sin(u)), show=False) # cartesian and parametric lines
Set title or other options:
>>> p10 = plot(x**2, title='second order polynomial', show=False)
Plot a list of expressions:
>>> p11 = plot([x, x**2, x**3], show=False)
>>> p12 = plot([x, x**2, x**3], (0,2), show=False) # explicit range
>>> p13 = plot([x*y, -x*y], show=False) # list of surfaces
And you can even plot a Plot or a Series object:
>>> a = plot(x, show=False)
>>> p14 = plot(a, show=False) # plotting a plot object
>>> p15 = plot(a[0], show=False) # plotting a series object
"""

plot_arguments = [p for p in args if isinstance(p, Plot)]
series_arguments = [p for p in args if isinstance(p, BaseSeries)]
args = sympify([np for np in args if not isinstance(np, (Plot, BaseSeries))])


# Are the arguments for only one plot or are they tuples with arguments
# for many plots. If it is the latter make the Tuples into list so they are
# mutable.
# args = (x, (x, 10, 20)) vs args = ((x, (x, 10, 20)), (y, (y, 20, 30)))
if all([isinstance(a, Tuple) for a in args]):
list_of_plots = map(list, args)
else:
list_of_plots = [list(args), ]

# Check for arguments containing lists of expressions for the same ranges.
# args = (x**2, (x, 10, 20)) vs args = ([x**3, x**2], (x, 10, 20))
list_arguments = [p for p in list_of_plots
if any(isinstance(a, list) for a in p)]
list_of_plots = [p for p in list_of_plots
if not any(isinstance(a, list) for a in p)]

def expand(plot):
"""
>> expand(([1,2,3],(1,2),(4,5)))
[[1, (1, 2), (4, 5)], [2, (1, 2), (4, 5)], [3, (1, 2), (4, 5)]]
"""
lists = [i for i in plot if isinstance(i, list)]
not_lists = [i for i in plot if not isinstance(i, list)]
return [list(l) + not_lists for l in zip(*lists)]

for a in list_arguments:
list_of_plots.extend(expand(a))

def add_variables_and_ranges(plot):
"""make sure all limits are in the form (symbol, a, b)"""

# find where the limits begin and expressions end
for i in range(len(plot)):
if isinstance(plot[i], Tuple):
break
else:
i = len(plot) + 1
exprs = list(plot[:i])
assert all(isinstance(e, Expr) for e in exprs)
assert all(isinstance(t, Tuple) for t in plot[i:])

ranges = set([i for i in plot[i:] if isinstance(i, Tuple) and len(i) > 1])
range_variables = set([t[0] for t in ranges if len(t) == 3])
expr_free = set_union(*[e.free_symbols for e in exprs if isinstance(e, Expr)])

default_range = Tuple(-10, 10)

# unambiguous cases for limits
# no ranges
if not ranges:
plot = exprs + [Tuple(e) + default_range for e in expr_free or [Dummy()]]

# all limits of length 3
elif all(len(i) == 3 for i in ranges):
pass

# all ranges the same
elif len(ranges) == 1:
range1 = ranges.pop()
if len(range1) == 2:
plot = exprs + [Tuple(x) + range1 for x in expr_free]

# ranges cover free variables of expression
elif expr_free < range_variables:
plot = exprs + [i if len(i) == 3 else Tuple(Dummy()) + i for i in ranges]

# ranges cover all but 1 free variable
elif len(expr_free - range_variables) == 1:
x = (expr_free - range_variables).pop()
ranges = list(ranges)
for i, ri in enumerate(ranges):
if len(ri) == 2:
ranges[i] = Tuple(x) + ri
break
else:
ranges.append(Tuple(x) + default_range)
for i, ri in enumerate(ranges):
if len(ri) == 2:
ranges[i] = Tuple(Dummy()) + ri
plot = exprs + ranges

# all implicit ranges
elif all(len(i) == 2 for i in ranges):
more = len(ranges) - len(expr_free)
all_free = list(expr_free) + [Dummy() for i in range(more)]
ranges = list(ranges)
ranges.extend(-more*[default_range])
plot = exprs + [Tuple(all_free[i]) + ri for i, ri in enumerate(ranges)]

else:
raise ValueError('erroneous or unanticipated range input')

return plot

list_of_plots = [Tuple(*add_variables_and_ranges(pl)) for pl in list_of_plots]

series_arguments.extend([OverloadedSeriesFactory(*pl) for pl in list_of_plots])

show = kwargs.pop('show', True)
p = Plot(*series_arguments, **kwargs)
for plot_argument in plot_arguments:
p.extend(plot_argument)
if show:
p.show()
return p


##############################################################################
# Data Series
##############################################################################
Expand Down Expand Up @@ -965,55 +783,6 @@ def get_meshes(self):
return (mesh_x, mesh_y, f(mesh_x, mesh_y))


### Factory class
class OverloadedSeriesFactory(BaseSeries):
""" Construct a data series representation that makes sense for the given
arguments. It works for a small subset of all possible plots.
"""
# overloading would be great here :(
# or the new function signature stuff from PEP 362
def __new__(cls, *args):
args = sympify(args)
if (len(args) == 2
and isinstance(args[0], Expr)
and isinstance(args[1], Tuple)
and len(args[1]) == 3):
inst = LineOver1DRangeSeries(*args)
elif (len(args) == 3
and isinstance(args[0], Expr)
and isinstance(args[1], Expr)
and isinstance(args[2], Tuple)
and len(args[2]) == 3):
inst = Parametric2DLineSeries(*args)
elif (len(args) == 3
and isinstance(args[0], Expr)
and isinstance(args[1], Tuple)
and isinstance(args[2], Tuple)
and len(args[1]) == 3
and len(args[2]) == 3):
inst = SurfaceOver2DRangeSeries(*args)
elif (len(args) == 4
and isinstance(args[0], Expr)
and isinstance(args[1], Expr)
and isinstance(args[2], Expr)
and isinstance(args[3], Tuple)
and len(args[3]) == 3):
inst = Parametric3DLineSeries(*args)
elif (len(args) == 5
and isinstance(args[0], Expr)
and isinstance(args[1], Expr)
and isinstance(args[2], Expr)
and isinstance(args[3], Tuple)
and isinstance(args[4], Tuple)
and len(args[3]) == 3
and len(args[4]) == 3):
inst = ParametricSurfaceSeries(*args)
else:
raise ValueError('The supplied argument do not correspond to a'
' valid plotable object.')
return inst


##############################################################################
# Backends
##############################################################################
Expand Down

0 comments on commit 866f93f

Please sign in to comment.