File size: 9,190 Bytes
b66f126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
========
Plotting
========

.. module:: sympy.plotting.plot

Introduction
------------

The plotting module allows you to make 2-dimensional and 3-dimensional plots.
Presently the plots are rendered using :external:mod:`matplotlib` as a
backend. It is also possible to plot 2-dimensional plots using a
:class:`~.TextBackend` if you do not have :external:mod:`matplotlib`.

The plotting module has the following functions:

* :func:`~.plot`: Plots 2D line plots.
* :func:`~.plot_parametric`: Plots 2D parametric plots.
* :func:`~.plot_implicit`: Plots 2D implicit and region plots.
* :func:`~.plot3d`: Plots 3D plots of functions in two variables.
* :func:`~.plot3d_parametric_line`: Plots 3D line plots, defined by a parameter.
* :func:`~.plot3d_parametric_surface`: Plots 3D parametric surface plots.

The above functions are only for convenience and ease of use. It is possible to
plot any plot by passing the corresponding ``Series`` class to :class:`~.Plot` as
argument.

Plot Class
----------

.. autoclass:: sympy.plotting.plot::Plot
   :members:

Plotting Function Reference
---------------------------

.. autofunction:: plot

.. autofunction:: plot_parametric

.. autofunction:: plot3d

.. autofunction:: plot3d_parametric_line

.. autofunction:: plot3d_parametric_surface

.. autofunction:: sympy.plotting.plot_implicit::plot_implicit

PlotGrid Class
--------------

.. autoclass:: sympy.plotting.plot::PlotGrid
   :members:

Series Classes
--------------

.. autoclass:: sympy.plotting.series::BaseSeries
   :members:

.. autoclass:: sympy.plotting.series::Line2DBaseSeries
   :members:

.. autoclass:: sympy.plotting.series::LineOver1DRangeSeries
   :members:

.. autoclass:: sympy.plotting.series::Parametric2DLineSeries
   :members:

.. autoclass:: sympy.plotting.series::Line3DBaseSeries
   :members:

.. autoclass:: sympy.plotting.series::Parametric3DLineSeries
   :members:

.. autoclass:: sympy.plotting.series::SurfaceBaseSeries
   :members:

.. autoclass:: sympy.plotting.series::SurfaceOver2DRangeSeries
   :members:

.. autoclass:: sympy.plotting.series::ParametricSurfaceSeries
   :members:

.. autoclass:: sympy.plotting.series::GenericDataSeries
   :members:

.. autoclass:: sympy.plotting.series::ImplicitSeries
   :members:

Backends
--------

.. autoclass:: sympy.plotting.plot::MatplotlibBackend
   :members:

.. autoclass:: sympy.plotting.plot::TextBackend
   :members:

Pyglet Plotting
---------------

.. module:: sympy.plotting.pygletplot

This is the documentation for the old plotting module that uses pyglet.
This module has some limitations and is not actively developed anymore.
For an alternative you can look at the new plotting module.

The pyglet plotting module 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 `pyglet <https://pyglet.org/>`_.

Here is the simplest usage:

    >>> from sympy import var
    >>> from sympy.plotting.pygletplot import PygletPlot as Plot
    >>> var('x y z')
    >>> Plot(x*y**3-y*x**3)

To see lots of plotting examples, see ``examples/pyglet_plotting.py`` and try running
it in interactive mode (``python -i plotting.py``)::

    $ python -i examples/pyglet_plotting.py

And type for instance ``example(7)`` or ``example(11)``.

See also the `Plotting Module <https://github.com/sympy/sympy/wiki/Plotting-capabilities>`_
wiki page for screenshots.


Plot Window Controls
--------------------

======================   ========
Camera                   Keys
======================   ========
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                     Keys
======================   ========
Toggle Visible           F5
Toggle Colors            F6
======================   ========

======================   ========
Window                   Keys
======================   ========
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

::

    >>> 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.

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:

    >>> 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]

Using the Interactive Interface
-------------------------------
::

    >>> 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()

Using Custom Color Functions
----------------------------

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[1] = fz, 'style=solid'
    >>> p[1].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) \rightarrow 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[1].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[1].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[1].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[1].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.

.. _plot_geom:

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 centred at
origin and of radius 2 units.
::

    >>> from sympy import *
    >>> x,y = symbols('x y')
    >>> plot_implicit(Eq(x**2+y**2, 4))

Similarly, :func:`~.plot_implicit` may be used to plot any 2-D geometric structure from
its implicit equation.

Plotting polygons (Polygon, RegularPolygon, Triangle) are not supported
directly.

Plotting with ASCII art
-----------------------

.. autofunction:: sympy.plotting.textplot::textplot