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