plot of Piecewise function not working

336 views
Skip to first unread message

bonc...@udel.edu

unread,
Mar 17, 2017, 4:20:07 PM3/17/17
to sympy
Plotting a Piecewise function fails.  I've pasted the markdown from my ipython notebook:

```python
from sympy import *
init_session()
```

    IPython console for SymPy 1.0 (Python 3.6.0-64-bit) (ground types: python)
    
    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols('x y z t')
    >>> k, m, n = symbols('k m n', integer=True)
    >>> f, g, h = symbols('f g h', cls=Function)
    >>> init_printing()
    
    Documentation can be found at http://docs.sympy.org/1.0/

```python
f = Piecewise((0,x<0), (1,x<1), (0,True))
print(f.subs(x,-0.5), f.subs(x,0.5), f.subs(x,1.5))
plot(f,(x,-1,2))
```

    0 1 0



    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/experimental_lambdify.py in __call__(self, args)
        193             #The result can be sympy.Float. Hence wrap it with complex type.
    --> 194             result = complex(self.lambda_func(args))
        195             if abs(result.imag) > 1e-7 * abs(result):


    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/experimental_lambdify.py in __call__(self, *args, **kwargs)
        310     def __call__(self, *args, **kwargs):
    --> 311         return self.lambda_func(*args, **kwargs)
        312 


    <string> in <lambda>(x0)


    TypeError: '<' not supported between instances of 'complex' and 'int'

    
    During handling of the above exception, another exception occurred:


    TypeError                                 Traceback (most recent call last)

    <ipython-input-2-447f720245d0> in <module>()
          1 f = Piecewise((0,x<0), (1,x<1), (0,True))
          2 print(f.subs(x,-0.5), f.subs(x,0.5), f.subs(x,1.5))
    ----> 3 plot(f,(x,-1,2))
    

    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/plot.py in plot(*args, **kwargs)
       1282     plots = Plot(*series, **kwargs)
       1283     if show:
    -> 1284         plots.show()
       1285     return plots
       1286 


    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/plot.py in show(self)
        183             self._backend.close()
        184         self._backend = self.backend(self)
    --> 185         self._backend.show()
        186 
        187     def save(self, path):


    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/plot.py in show(self)
       1016 
       1017     def show(self):
    -> 1018         self.process_series()
       1019         #TODO after fixing https://github.com/ipython/ipython/issues/1255
       1020         # you can uncomment the next line and remove the pyplot.show() call


    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/plot.py in process_series(self)
        895             # Create the collections
        896             if s.is_2Dline:
    --> 897                 collection = self.LineCollection(s.get_segments())
        898                 self.ax.add_collection(collection)
        899             elif s.is_contour:


    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/plot.py in get_segments(self)
        501                     list_segments.append([p, q])
        502 
    --> 503             f_start = f(self.start)
        504             f_end = f(self.end)
        505             sample([self.start, f_start], [self.end, f_end], 0)


    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/experimental_lambdify.py in __call__(self, args)
        227                                                     use_evalf=True,
        228                                                     complex_wrap_evalf=True)
    --> 229                 result = self.lambda_func(args)
        230                 warnings.warn('The evaluation of the expression is'
        231                         ' problematic. We are trying a failback method'


    /Users/boncelet/anaconda/lib/python3.6/site-packages/sympy/plotting/experimental_lambdify.py in __call__(self, *args, **kwargs)
        309 
        310     def __call__(self, *args, **kwargs):
    --> 311         return self.lambda_func(*args, **kwargs)
        312 
        313 


    <string> in <lambda>(x0)


    TypeError: '<' not supported between instances of 'complex' and 'int'



```python

```

Z Cart.

unread,
Sep 14, 2017, 1:03:14 AM9/14/17
to sympy
Hello, were you ever able to get this to work? I am having the same problem with plotting a piecewise function!

Jia Guo

unread,
Feb 22, 2018, 3:46:05 PM2/22/18
to sympy
plot(f,(x,-1,2),adaptive=False, num_of_points=200)
 
this should work

Leonid Kovalev

unread,
Feb 22, 2018, 5:24:53 PM2/22/18
to sympy
The code in the original message works as is in the current development version of SymPy (which can be obtained by cloning from GitHub.) The issue was fixed by https://github.com/sympy/sympy/pull/13826 



On Friday, March 17, 2017 at 4:20:07 PM UTC-4, bonc...@udel.edu wrote:
Reply all
Reply to author
Forward
0 new messages