prepare manual for rest conversion
[PyX/mjg.git] / examples / graphs / lissajous.txt
blobd2228cabcee9dba72688fcc0e26faf70a9eb4da7
1 Plotting a parametric function
3 This example shows how to use a `paramfunction` as a data source for a graph.
4 `paramfunction` defines a parameter variable name given as a string in the first
5 argument, a minimal and a maximal value given as the second and third argument
6 and an expression string as forth argument. ... This expression string assigns a
7 tuple of expressions to a tuple of data names. As usual the styles will decide
8 what those data names are responsible for.
10 ! Like for the function, you can also access external data and functions in your
11 expression. Suppose we want to provide the data for this example by means of
12 the following python function:
14     def lissajousdata(k):
15         return sin(2*k), cos(3*k)
17 Then we would need to modify the plot command to:
19     g.plot(graph.data.paramfunction("t", min, max, "x, y = f(t)",
20                                     context={"f": lissajousdata}))
22 Note, that `t` and `f` in the string expression stand for `k` and `lissajousdata`
23 respectively. You can also use the same names in the expression if you like.
25 ! When you try this, you will notice, that you need to add `sin` and `cos` to the
26 list of imported symbols from the math module. While this should be expected,
27 it is more interesting to note, that it was not necessary to import those
28 functions before. The reason is, that the string expressions are evaluated in
29 a certain context. This context does not only contain the symbols given in the
30 `context` keyword argument, but in addition some standard mathematical functions
31 (and constants) including `sin` and `cos`. See the reference manual for a complete
32 list.
34 For the common case of plotting a parametric function providing data for `x` and
35 `y` by means of an external function, there is a special `paramfunctionxy` which
36 directly calls the external function instead of using a context in a string
37 expression. A `lambda` expression could serve to provide the data inline, which
38 results in:
40     g.plot(graph.data.paramfunctionxy(lambda k: (sin(2*k), cos(3*k)), 0, 2*pi))
42 Note that the external expression is provided in the first argument here.
43 Since the system just need to call the function, the parameter variable name
44 does not have to be provided to `paramfunctionxy`.