{ "metadata": { "name": "plot_intro" }, "nbformat": 2, "worksheets": [ { "cells": [ { "cell_type": "markdown", "source": [ "#New Plotting Framework for SymPy" ] }, { "cell_type": "markdown", "source": [ "", "", "## Structure of the Module", "", "", "", "This module implements a new plotting framework for SymPy. The central class of the module is the `Plot` class that connects the data representations (subclasses of `BaseSeries`) with different plotting backends. It's not imported by default for backward compatibility with the old module.", "", "", "", "Then there are the `plot_*()` functions for plotting different kinds of plots and is better suited for interactive work.", "", "", "", "* ``plot``: Plots line plots in 2D.", "", "* ``plot_parametric``: Plots parametric line plots in 2D.", "", "* ``plot_implicit`` : Plots implicit equations and region plots in 2D", "", "* ``plot3d`` : Plots functions of two variables in 3D", "", "* ``plot3d_parametric_line``: Plots line parametric plots in 3D", "", "* ``plot3d_parametric_surface`` : Plots surface parametric plots of functions with two variables in 3D." ] }, { "cell_type": "markdown", "source": [ "##General examples" ] }, { "cell_type": "code", "collapsed": true, "input": [ "from sympy.plotting import plot, plot_parametric, plot3d, plot3d_parametric_line, plot3d_parametric_surface" ], "language": "python", "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "p = plot(x)" ], "language": "python", "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "p # the Plot object" ], "language": "python", "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "p[0] # one of the data series objects" ], "language": "python", "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "p[0].label # an option of the data series" ], "language": "python", "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "p.legend # a global option of the plot" ], "language": "python", "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "p.legend = True", "", "p.show()" ], "language": "python", "outputs": [], "prompt_number": 14 }, { "cell_type": "markdown", "source": [ "You can plot 2D different functions in the same plot." ] }, { "cell_type": "code", "collapsed": false, "input": [ "p1 = plot_parametric(x*sin(x),x*cos(x), show=False)", "", "p1.extend(p) # Plot objects are just like lists.", "", "p1.show()" ], "language": "python", "outputs": [], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "p1.legend = True", "", "p1.show()" ], "language": "python", "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "p1[0].line_color='r'", "", "p1[1].line_color='b' # a constant color", "", "p1.show()" ], "language": "python", "outputs": [], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "p1[0].line_color = lambda a : a # color dependent on the parameter", "", "p1.show()" ], "language": "python", "outputs": [], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "p1.title = 'Big title'", "", "p1.xlabel = 'the x axis'", "", "p1[1].label = 'straight line'", "", "p1.show()" ], "language": "python", "outputs": [], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "p1.aspect_ratio" ], "language": "python", "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "p1.aspect_ratio = (1,1)", "", "p1.xlim = (-15,20)", "", "p1.show()" ], "language": "python", "outputs": [], "prompt_number": 21 }, { "cell_type": "markdown", "source": [ "Hm, `xlim` does not work in the notebook. Hopefully it works in IPython." ] }, { "cell_type": "code", "collapsed": true, "input": [ "p1._backend.ax.get_xlim()" ], "language": "python", "outputs": [], "prompt_number": 17 }, { "cell_type": "markdown", "source": [ "Yeah, the backend got the command, but the `inline` backend does not honour it." ] }, { "cell_type": "markdown", "source": [ "## Adding expressions to a plot" ] }, { "cell_type": "code", "collapsed": false, "input": [ "p = plot(x)", "", "p" ], "language": "python", "outputs": [], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "p.extend(plot(x+1, show=False))", "", "p.show()", "", "p" ], "language": "python", "outputs": [], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "p.append(plot(x+3, x**2, show=False)[1])", "", "p.show()", "", "p" ], "language": "python", "outputs": [], "prompt_number": 25 }, { "cell_type": "markdown", "source": [ "## Different types of plots" ] }, { "cell_type": "markdown", "source": [ "###``plot``", "", "The ``plot`` by default uses an recursive adaptive algorithm to plot line plots. The default depth of recursion is 12, which means the function will be sampled at a maximum of $2^{12}$ points." ] }, { "cell_type": "code", "collapsed": false, "input": [ "help(plot)" ], "language": "python", "outputs": [], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "plot(sin(x**2)) # plots with adaptive sampling and default range of (-10, 10)" ], "language": "python", "outputs": [], "prompt_number": 28 }, { "cell_type": "markdown", "source": [ "You can also specify the depth of the recursion. It is also possible to disable adaptive sampling and use uniform sampling with ``nb_of_points``." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot(sin(x**2), depth=7) #specifying the depth of recursion." ], "language": "python", "outputs": [], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "plot(sin(x**2), adaptive=False, nb_of_points=500)" ], "language": "python", "outputs": [], "prompt_number": 31 }, { "cell_type": "markdown", "source": [ "###``plot_parametric``", "", "``plot_parametric`` uses an recursive adaptive sampling algorithm to plot." ] }, { "cell_type": "code", "collapsed": false, "input": [ "help(plot_parametric)" ], "language": "python", "outputs": [], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "plot_parametric(cos(x), sin(x))" ], "language": "python", "outputs": [], "prompt_number": 33 }, { "cell_type": "markdown", "source": [ "Multiple plots." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot_parametric((cos(x), sin(x)), (x, cos(x)))" ], "language": "python", "outputs": [], "prompt_number": 34 }, { "cell_type": "markdown", "source": [ "We can combine parametric and line plots into a single plot." ] }, { "cell_type": "code", "collapsed": false, "input": [ "p = plot(sin(x), show=False)", "", "", "", "p.extend(plot_parametric(cos(x), sin(x), show=False))", "", "p.show()" ], "language": "python", "outputs": [], "prompt_number": 35 }, { "cell_type": "markdown", "source": [ "###``plot3d``" ] }, { "cell_type": "code", "collapsed": false, "input": [ "help(plot3d)" ], "language": "python", "outputs": [], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "plot3d(x*y)" ], "language": "python", "outputs": [], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "plot3d(x*y, nb_of_points_x=100, nb_of_points_y=50) " ], "language": "python", "outputs": [], "prompt_number": 38 }, { "cell_type": "markdown", "source": [ "###``plot3_parametric_line``" ] }, { "cell_type": "code", "collapsed": false, "input": [ "help(plot3d_parametric_line)" ], "language": "python", "outputs": [], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "plot3d_parametric_line(cos(x), sin(x), x)" ], "language": "python", "outputs": [], "prompt_number": 40 }, { "cell_type": "markdown", "source": [ "###``plot3d_parametric_surface``" ] }, { "cell_type": "code", "collapsed": false, "input": [ "help(plot3d_parametric_surface)" ], "language": "python", "outputs": [], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "plot3d_parametric_surface(cos(x + y), sin(x - y), x - y)" ], "language": "python", "outputs": [], "prompt_number": 42 }, { "cell_type": "markdown", "source": [ "## Complex values", "", "If complex values are encountered, they are discarded while plotting." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot(sqrt(x), (x, -5, 5))" ], "language": "python", "outputs": [], "prompt_number": 43 }, { "cell_type": "markdown", "source": [ "", "", "## Textplot", "", "", "", "There is also the textplot backend that permits plotting in the terminal." ] }, { "cell_type": "code", "collapsed": true, "input": [ "pt = plot(sin(x),show=False)" ], "language": "python", "outputs": [], "prompt_number": 44 }, { "cell_type": "code", "collapsed": true, "input": [ "pt.backend = plot_backends['text']" ], "language": "python", "outputs": [], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "pt.show()" ], "language": "python", "outputs": [], "prompt_number": 46 } ] } ] }