SymPy can do nice 2D and 3D plots that can be controlled by console commands as well as keyboard and mouse, with the only dependency being ctypes, which is included in Python2.5 (you need to install it in Python2.4).
Here is the simplest usage:
>>> from sympy import var, Plot >>> var('x y z') >>> Plot(x*y**3-y*x**3)
To see lots of plotting examples, see examples/plotting.py and try running it in interactive mode (python -i plotting.py):
$ python -i examples/plotting.py
And type for instance example(7) or example(11).
See also the Plotting Module wiki page for screenshots.
|Zoom||R and F, Page Up and Down, Numpad + and -|
|Rotate View X,Y axis||Arrow Keys, A,S,D,W, Numpad 4,6,8,2|
|Rotate View Z axis||Q and E, Numpad 7 and 9|
|Rotate Ordinate Z axis||Z and C, Numpad 1 and 3|
|Reset||X, Numpad 5|
The mouse can be used to rotate, zoom, and translate by dragging the left, middle, and right mouse buttons respectively.
Plot supports several curvilinear coordinate modes, and they are independent for each plotted function. You can specify a coordinate mode explicitly with the ‘mode’ named argument, but it can be automatically determined for cartesian or parametric plots, and therefore must only be specified for polar, cylindrical, and spherical modes.
Specifically, Plot(function arguments) and Plot.__setitem__(i, function arguments) (accessed using array-index syntax on the Plot instance) will interpret your arguments as a cartesian plot if you provide one function and a parametric plot if you provide two or three functions. Similarly, the arguments will be interpreted as a curve is one variable is used, and a surface if two are used.
Supported mode names by number of variables:
>>> Plot(1, 'mode=spherical; color=zfade4')
Note that function parameters are given as option strings of the form “key1=value1; key2 = value2” (spaces are truncated). Keyword arguments given directly to plot apply to the plot itself.
The basic format for variable intervals is [var, min, max, steps]. However, the syntax is quite flexible, and arguments not specified are taken from the defaults for the current coordinate mode:
>>> Plot(x**2) # implies [x,-5,5,100] >>> Plot(x**2, , ) # [x,-1,1,40], [y,-1,1,40] >>> Plot(x**2-y**2, , ) # [x,-1,1,100], [y,-1,1,100] >>> Plot(x**2, [x,-13,13,100]) >>> Plot(x**2, [-13,13]) # [x,-13,13,100] >>> Plot(x**2, [x,-13,13]) # [x,-13,13,100] >>> Plot(1*x, , [x], 'mode=cylindrical') # [unbound_theta,0,2*Pi,40], [x,-1,1,20]
>>> p = Plot(visible=False) >>> f = x**2 >>> p = f >>> p = f.diff(x) >>> p = f.diff(x).diff(x) >>> p : x**2, 'mode=cartesian' : 2*x, 'mode=cartesian' : 2, 'mode=cartesian' >>> p.show() >>> p.clear() >>> p <blank plot> >>> p = x**2+y**2 >>> p.style = 'solid' >>> p = -x**2-y**2 >>> p.style = 'wireframe' >>> p.color = z, (0.4,0.4,0.9), (0.9,0.4,0.4) >>> p.style = 'both' >>> p.style = 'both' >>> p.close()
The following code plots a saddle and color it by the magnitude of its gradient:
>>> fz = x**2-y**2 >>> Fx, Fy, Fz = fz.diff(x), fz.diff(y), 0 >>> p = fz, 'style=solid' >>> p.color = (Fx**2 + Fy**2 + Fz**2)**(0.5)
The coloring algorithm works like this:
When not specified explicitly, the default color gradient is f(0.0)=(0.4,0.4,0.4) -> f(1.0)=(0.9,0.9,0.9). In our case, everything is gray-scale because we have applied the default color gradient uniformly for each color component. When defining a color scheme in this way, you might want to supply a color gradient as well:
>>> p.color = (Fx**2 + Fy**2 + Fz**2)**(0.5), ................ (0.1,0.1,0.9), (0.9,0.1,0.1)
Here’s a color gradient with four steps:
>>> gradient = [ 0.0, (0.1,0.1,0.9), 0.3, (0.1,0.9,0.1), ................ 0.7, (0.9,0.9,0.1), 1.0, (1.0,0.0,0.0) ] >>> p.color = (Fx**2 + Fy**2 + Fz**2)**(0.5), gradient
The other way to specify a color scheme is to give a separate function for each component r, g, b. With this syntax, the default color scheme is defined:
>>> p.color = z,y,x, (0.4,0.4,0.4), (0.9,0.9,0.9)
This maps z->red, y->green, and x->blue. In some cases, you might prefer to use the following alternative syntax:
>>> p.color = z,(0.4,0.9), y,(0.4,0.9), x,(0.4,0.9)
You can still use multi-step gradients with three-function color schemes.
The plotting module is capable of plotting some 2D geometric entities like line, circle and ellipse. The following example plots a circle and a tangent line at a random point on the ellipse.
In : p = Plot(axes='label_axes=True') In : c = Circle(Point(0,0), 1) In : t = c.tangent_line(c.random_point()) In : p = c In : p = t
Plotting polygons (Polygon, RegularPolygon, Triangle) are not supported directly. However a polygon can be plotted through a loop as follows.
In : p = Plot(axes='label_axes=True') In : t = RegularPolygon(Point(0,0), 1, 5) In : for i in range(len(t.sides)): ....: p[i] = t.sides[i]