Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Old Plotting Module [Pyglet based]
Clone this wiki locally
Table of Contents
With the plotting module, you can simultaneously view any number of function plots in a 3D environment controlled by console commands as well as the keyboard and mouse. The only external dependency is ctypes, which is included in Python 2.5 (you will need to install it for Python 2.3 or 2.4, and it is not available for earlier versions). Here is the simplest usage:
>>> from sympy import symbols, Plot >>> x,y,z = symbols('xyz') >>> 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).
|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.(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:
- 1 (curves): parametric, cartesian, polar
- 2 (surfaces): parametric, cartesian, cylindrical, spherical
>>> 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,]. 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:
# Evaluate the color function(s) across the curve or surface. # Find the minimum and maximum value of each component. # Scale each component to the color gradient.
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)
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]