From fb25ad3b398a8e9cd2ad1366813379743f7b4387 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B6rg=20Lehmann?= Date: Sun, 21 May 2006 15:16:32 +0000 Subject: [PATCH] more graphs example work git-svn-id: https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx@2745 069f4177-920e-0410-937b-c2a4a81bcd90 --- examples/graphs/INDEX | 1 + examples/graphs/function.py | 6 ++++++ examples/graphs/function.txt | 32 ++++++++++++++++++++++++++++++++ examples/graphs/lissajous.txt | 2 +- examples/graphs/minimal.py | 11 ----------- examples/graphs/minimal.txt | 36 ++++++++++++++++++++++++++++++++++-- 6 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 examples/graphs/function.py create mode 100644 examples/graphs/function.txt diff --git a/examples/graphs/INDEX b/examples/graphs/INDEX index 318cb967..c3855ea6 100644 --- a/examples/graphs/INDEX +++ b/examples/graphs/INDEX @@ -1,4 +1,5 @@ minimal +function errorbar histogram lissajous diff --git a/examples/graphs/function.py b/examples/graphs/function.py new file mode 100644 index 00000000..66a36ec7 --- /dev/null +++ b/examples/graphs/function.py @@ -0,0 +1,6 @@ +from pyx import * + +g = graph.graphxy(width=8) +g.plot(graph.data.function("y(x)=sin(x)/x", min=-15, max=15)) +g.writeEPSfile("function") +g.writePDFfile("function") diff --git a/examples/graphs/function.txt b/examples/graphs/function.txt new file mode 100644 index 00000000..998c1b26 --- /dev/null +++ b/examples/graphs/function.txt @@ -0,0 +1,32 @@ +Plotting a mathematical function + +Here, we explain how to plot the graph of a real-valued mathematical function of +a single real parameter. ... + +As in the previous example, we first create a `graph.graphxy` instance passing +the width as argument. We then pass a `graph.data.function` instance to the +`plot` method of the graph. As first argument, we pass the function in the form +"y(x) = f(x)". Here, the left-hand side of the equation defines which parameter +is independent (here: `x`) and which one is dependent (here: `y`). We also need +to inform PyX about the range of the independent variable. This is done by passing +the parameters `min` and `max`. + +! In order to increase the resolution of the function graph, you can use the +parameter `points` of the `graph.data.function` class to increase the number of +sampling points from its default value of `100`. + +Note that the default graph style for function data is `graph.style.line` synce +PyX assumes a continuous x-range. + +! You only need to pass the `min` and `max` parameters to the +`graph.data.function` class, if PyX cannot figure out an axis range by itself. +Thus, an alternative way to achieve the above result would look like + g = graph.graphxy(width=8, x=graph.axis.linear(min=-15, max=15)) + g.plot(graph.data.function("y(x)=sin(x)/x")) +Here, we explicitely define an x-axis range by passing an appropriate +argument to the `x` parameter of the `graph.graphxy` class. + +! For PyX it does not matter, whether you plot a function of a parameter `x` or +a parameter `y`. Thus, you may as well write + g.plot(graph.data.function("x(y)=sin(y)/y", min=-15, max=15)) +to obtain a plot where the y-coordinate is the independent one. diff --git a/examples/graphs/lissajous.txt b/examples/graphs/lissajous.txt index c25509f8..5b042b47 100644 --- a/examples/graphs/lissajous.txt +++ b/examples/graphs/lissajous.txt @@ -5,7 +5,7 @@ This example shows how to use a `paramfunction` as a data source for a graph. argument, a minimal and a maximal value given as the second and third argument and an expression string as forth argument. ... This expression string assigns a tuple of expressions to a tuple of data names. As usual the styles will decide -#what those data names are responsible for. +what those data names are responsible for. ! Like for the function, you can also access external data and functions in your expression. Suppose we want to provide the data for this example by means of diff --git a/examples/graphs/minimal.py b/examples/graphs/minimal.py index 8c1d47fc..4aea09c7 100644 --- a/examples/graphs/minimal.py +++ b/examples/graphs/minimal.py @@ -4,14 +4,3 @@ g = graph.graphxy(width=8) g.plot(graph.data.file("minimal.dat", x=1, y=2)) g.writeEPSfile("minimal") g.writePDFfile("minimal") - -# the file minimal.dat looks like: -# 1 2 -# 2 3 -# 3 8 -# 4 13 -# 5 18 -# 6 21 - -# graph styles can be modified by a second parameter to the plot method: -# g.plot(graph.data.file("minimal.dat", x=1, y=2), [graph.style.line()]) diff --git a/examples/graphs/minimal.txt b/examples/graphs/minimal.txt index 730c44be..2baa04f3 100644 --- a/examples/graphs/minimal.txt +++ b/examples/graphs/minimal.txt @@ -1,6 +1,15 @@ -Basic graph drawing: data contained in a file +Plotting data contained in a file This example shows how to draw a graph representing data stored in a file. ... +We assume that the data is arranged in the file `minimal.dat` in a whitespace-separated +two-column form: + # minimal.dat + 1 2 + 2 3 + 3 8 + 4 13 + 5 18 + 6 21 The first step is to create an instance of the `graphxy` class which can be found in the `graph` module. By convention, we call it `g`. The constructor @@ -18,10 +27,33 @@ the keyword arguments `x=1` and `y=2`, which tell PyX that the first (second) column of the file contains the x (y) values. The `graph.data.file` instance is then directly passed to the `plot` method of the graph `g`. +! Note that PyX by default ignores comments starting by a # sign when reading +in the data from the file. + +!! The previous statement is actually not completely correct, as PyX +uses the last comment preceding the actual data to give names to the columns. +Thus, for a file looking like + # my data (this line is ignored by PyX, but not the following) + # x y + 1 2 + ... +you wouldn't need to label the columns in the `graph.data.file` call at all. + Finally, we write the graph to an EPS and PDF file. Here, we use that every graph is (by inheritance) an instance of the `canvas` class, as well, such that we can directly write it into a file. ! Of course, you can also insert a graph into another canvas and write this canvas later to a file. This way, you can, for instance, easily arrange more -than one graph on a page. +than one graph on a page. Later examples will make use of this fact. + +In PyX, the way data is plotted in a graph is defined by a so-called graph +style. A couple of standard graph styles are contained in the module +`graph.style`. Depending on the data source, PyX chooses a default style. Here, +we are taking the data from a file and PyX assumes that the values represent a +discrete set of data points. Hence, it chooses the symbol style +`graph.style.symbol` to plot the data. To override this default behaviour, you +can pass a list of styles as second argument to the `plot` method. For instance, +to have PyX drawing a line through the data points, you can use + + g.plot(graph.data.file("minimal.dat", x=1, y=2), [graph.style.line()]) -- 2.11.4.GIT