(ctx, f, xlim=[-5,5], ylim=None, points=200, file=None, dpi=None,
    singularities=[], axes=None)
r"""
    Shows a simple 2D plot of a function `f(x)` or list of functions
    `[f_0(x), f_1(x), \ldots, f_n(x)]` over a given interval
    specified by *xlim*. Some examples::

        plot(lambda x: exp(x)*li(x), [1, 4])
        plot([cos, sin], [-4, 4])
        plot([fresnels, fresnelc], [-4, 4])
        plot([sqrt, cbrt], [-4, 4])
        plot(lambda t: zeta(0.5+t*j), [-20, 20])
        plot([floor, ceil, abs, sign], [-5, 5])

    Points where the function raises a numerical exception or
    returns an infinite value are removed from the graph.
    Singularities can also be excluded explicitly
    as follows (useful for removing erroneous vertical lines)::

        plot(cot, ylim=[-5, 5])   # bad
        plot(cot, ylim=[-5, 5], singularities=[-pi, 0, pi])  # good

    For parts where the function assumes complex values, the
    real part is plotted with dashes and the imaginary part
    is plotted with dots.

    .. note :: This function requires matplotlib (pylab).
r"""
    Shows a simple 2D plot of a function `f(x)` or list of functions
    `[f_0(x), f_1(x), \ldots, f_n(x)]` over a given interval
    specified by *xlim*. Some examples::
r Shows a simple 2D plot of a function f ( x ) or list of functions [ f_0 ( x ) f_1 ( x ) \ ldots f_n ( x ) ] over a given interval specified by * xlim * . Some examples ::
def plot(ctx, f, xlim=[-5,5], ylim=None, points=200, file=None, dpi=None,
    singularities=[], axes=None):
    r"""
    Shows a simple 2D plot of a function `f(x)` or list of functions
    `[f_0(x), f_1(x), \ldots, f_n(x)]` over a given interval
    specified by *xlim*. Some examples::

        plot(lambda x: exp(x)*li(x), [1, 4])
        plot([cos, sin], [-4, 4])
        plot([fresnels, fresnelc], [-4, 4])
        plot([sqrt, cbrt], [-4, 4])
        plot(lambda t: zeta(0.5+t*j), [-20, 20])
        plot([floor, ceil, abs, sign], [-5, 5])

    Points where the function raises a numerical exception or
    returns an infinite value are removed from the graph.
    Singularities can also be excluded explicitly
    as follows (useful for removing erroneous vertical lines)::

        plot(cot, ylim=[-5, 5])   # bad
        plot(cot, ylim=[-5, 5], singularities=[-pi, 0, pi])  # good

    For parts where the function assumes complex values, the
    real part is plotted with dashes and the imaginary part
    is plotted with dots.

    .. note :: This function requires matplotlib (pylab).
    """
    if file:
        axes = None
    fig = None
    if not axes:
        import pylab
        fig = pylab.figure()
        axes = fig.add_subplot(111)
    if not isinstance(f, (tuple, list)):
        f = [f]
    a, b = xlim
    colors = ['b', 'r', 'g', 'm', 'k']
    for n, func in enumerate(f):
        x = ctx.arange(a, b, (b-a)/float(points))
        segments = []
        segment = []
        in_complex = False
        for i in xrange(len(x)):
            try:
                if i != 0:
                    for sing in singularities:
                        if x[i-1] <= sing and x[i] >= sing:
                            raise ValueError
                v = func(x[i])
                if ctx.isnan(v) or abs(v) > 1e300:
                    raise ValueError
                if hasattr(v, "imag") and v.imag:
                    re = float(v.real)
                    im = float(v.imag)
                    if not in_complex:
                        in_complex = True
                        segments.append(segment)
                        segment = []
                    segment.append((float(x[i]), re, im))
                else:
                    if in_complex:
                        in_complex = False
                        segments.append(segment)
                        segment = []
                    if hasattr(v, "real"):
                        v = v.real
                    segment.append((float(x[i]), v))
            except ctx.plot_ignore:
                if segment:
                    segments.append(segment)
                segment = []
        if segment:
            segments.append(segment)
        for segment in segments:
            x = [s[0] for s in segment]
            y = [s[1] for s in segment]
            if not x:
                continue
            c = colors[n % len(colors)]
            if len(segment[0]) == 3:
                z = [s[2] for s in segment]
                axes.plot(x, y, '--'+c, linewidth=3)
                axes.plot(x, z, ':'+c, linewidth=3)
            else:
                axes.plot(x, y, c, linewidth=3)
    axes.set_xlim([float(_) for _ in xlim])
    if ylim:
        axes.set_ylim([float(_) for _ in ylim])
    axes.set_xlabel('x')
    axes.set_ylabel('f(x)')
    axes.grid(True)
    if fig:
        if file:
            pylab.savefig(file, dpi=dpi)
        else:
            pylab.show()
def plot ( ctx , f , xlim = [ - 5 , 5 ] , ylim = None , points = 200 , file = None , dpi = None , singularities = [ ] , axes = None ) : if file : axes = None fig = None if not axes : import pylab fig = pylab . figure ( ) axes = fig . add_subplot ( 111 ) if not isinstance ( f , ( tuple , list ) ) : f = [ f ] a , b = xlim colors = [ 'b' , 'r' , 'g' , 'm' , 'k' ] for n , func in enumerate ( f ) : x = ctx . arange ( a , b , ( b - a ) / float ( points ) ) segments = [ ] segment = [ ] in_complex = False for i in xrange ( len ( x ) ) : try : if i != 0 : for sing in singularities : if x [ i - 1 ] <= sing and x [ i ] >= sing : raise ValueError v = func ( x [ i ] ) if ctx . isnan ( v ) or abs ( v ) > 1e300 : raise ValueError if hasattr ( v , "imag" ) and v . imag : re = float ( v . real ) im = float ( v . imag ) if not in_complex : in_complex = True segments . append ( segment ) segment = [ ] segment . append ( ( float ( x [ i ] ) , re , im ) ) else : if in_complex : in_complex = False segments . append ( segment ) segment = [ ] if hasattr ( v , "real" ) : v = v . real segment . append ( ( float ( x [ i ] ) , v ) ) except ctx . plot_ignore : if segment : segments . append ( segment ) segment = [ ] if segment : segments . append ( segment ) for segment in segments : x = [ s [ 0 ] for s in segment ] y = [ s [ 1 ] for s in segment ] if not x : continue c = colors [ n % len ( colors ) ] if len ( segment [ 0 ] ) == 3 : z = [ s [ 2 ] for s in segment ] axes . plot ( x , y , '--' + c , linewidth = 3 ) axes . plot ( x , z , ':' + c , linewidth = 3 ) else : axes . plot ( x , y , c , linewidth = 3 ) axes . set_xlim ( [ float ( _ ) for _ in xlim ] ) if ylim : axes . set_ylim ( [ float ( _ ) for _ in ylim ] ) axes . set_xlabel ( 'x' ) axes . set_ylabel ( 'f(x)' ) axes . grid ( True ) if fig : if file : pylab . savefig ( file , dpi = dpi ) else : pylab . show ( )