Plotting Module
From SymPy
Contents |
Introduction
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).
Plot Window Controls
| Camera | |
| Sensitivity Modifier | SHIFT |
| 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 |
| View XY | F1 |
| View XZ | F2 |
| View YZ | F3 |
| View Perspective | F4 |
| Reset | X, Numpad 5 |
| Axes | |
| Toggle Visible | F5 |
| Toggle Colors | F6 |
| Window | |
| Close | ESCAPE |
| Screenshot | F8 |
The mouse can be used to rotate, zoom, and translate by dragging the left, middle, and right mouse buttons respectively.
Coordinate Modes
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:
- 1 (curves): parametric, cartesian, polar
- 2 (surfaces): parametric, cartesian, cylindrical, spherical
<source lang="python"> >>> Plot(1, 'mode=spherical; color=zfade4') </source>
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.
Specifying Intervals for Variables
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:
<source lang="python"> >>> Plot(x**2) # implies [x,-5,5,100] >>> Plot(x**2, [], []) # [x,-1,1,40], [y,-1,1,40] >>> Plot(x**2-y**2, [100], [100]) # [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] </source>
Using the Interactive Interface
<source lang="python"> >>> p = Plot(visible=False) >>> f = x**2 >>> p[1] = f >>> p[2] = f.diff(x) >>> p[3] = f.diff(x).diff(x) >>> p [1]: x**2, 'mode=cartesian' [2]: 2*x, 'mode=cartesian' [3]: 2, 'mode=cartesian' >>> p.show() >>> p.clear() >>> p <blank plot> >>> p[1] = x**2+y**2 >>> p[1].style = 'solid' >>> p[2] = -x**2-y**2 >>> p[2].style = 'wireframe' >>> p[1].color = z, (0.4,0.4,0.9), (0.9,0.4,0.4) >>> p[1].style = 'both' >>> p[2].style = 'both' >>> p.close() </source>
Using Custom Color Functions
The following code plots a saddle and color it by the magnitude of its gradient:
<source lang="python">
>>> fz = x**2-y**2
>>> Fx, Fy, Fz = fz.diff(x), fz.diff(y), 0
>>> p[1] = fz, 'style=solid'
>>> p[1].color = (Fx**2 + Fy**2 + Fz**2)**(0.5)
</source>
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:
<source lang="python">
>>> p[1].color = (Fx**2 + Fy**2 + Fz**2)**(0.5),
................ (0.1,0.1,0.9), (0.9,0.1,0.1)
</source>
Here's a color gradient with four steps:
<source lang="python">
>>> 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[1].color = (Fx**2 + Fy**2 + Fz**2)**(0.5), gradient
</source>
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:
<source lang="python">
>>> p[1].color = z,y,x, (0.4,0.4,0.4), (0.9,0.9,0.9)
</source>
This maps z->red, y->green, and x->blue. In some cases, you might prefer to use the following alternative syntax: <source lang="python"> >>> p[1].color = z,(0.4,0.9), y,(0.4,0.9), x,(0.4,0.9) </source> You can still use multi-step gradients with three-function color schemes.
Plotting Geometric Entities
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. <source lang="python"> In [1]: p = Plot(axes='label_axes=True')
In [2]: c = Circle(Point(0,0), 1)
In [3]: t = c.tangent_line(c.random_point())
In [4]: p[0] = c
Plotting polygons (Polygon, RegularPolygon, Triangle) are not supported directly. However a polygon can be plotted through a loop as follows. <source lang="python"> In [6]: p = Plot(axes='label_axes=True')
In [7]: t = RegularPolygon(Point(0,0), 1, 5)
In [8]: for i in range(len(t.sides)):
....: p[i] = t.sides[i]




