1 Plotting data contained in a file
3 This example shows how to draw a graph representing data stored in a file. ...
4 We assume that the data is arranged in the file `minimal.dat` in a whitespace-separated
14 The first step is to create an instance of the `graphxy` class which can be
15 found in the `graph` module. By convention, we call it `g`. The constructor
16 expects at least some information about the desired size of the graph. Here, we
17 specify a width of 8 cm.
19 ! If we only specify one dimension of the graph size, PyX calculates the other
20 automatically, assuming a ratio corresponding to the golden ratio.
22 Next, we add some data to the yet empty graph. In order to do so, we first
23 create a `graph.data.file` instance, which reads the file with the name given
24 as the first argument, i.e., in the present case, `"minimal.dat"`. In addition,
25 we have to specify, how the data is organized in the file. To this end, we use
26 the keyword arguments `x=1` and `y=2`, which tell PyX that the first (second)
27 column of the file contains the x (y) values. The `graph.data.file` instance is
28 then directly passed to the `plot` method of the graph `g`.
30 ! Note that PyX by default ignores comments starting by a # sign when reading
31 in the data from the file.
33 !! The previous statement is actually not completely correct, as PyX
34 uses the last comment preceding the actual data to give names to the columns.
35 Thus, for a file looking like
36 # my data (this line is ignored by PyX, but not the following)
40 you wouldn't need to label the columns in the `graph.data.file` call at all.
42 Finally, we write the graph to an EPS and PDF file. Here, we use that every
43 graph is (by inheritance) an instance of the `canvas` class, as well, such that
44 we can directly write it into a file.
46 ! Of course, you can also insert a graph into another canvas and write this
47 canvas later to a file. This way, you can, for instance, easily arrange more
48 than one graph on a page. Later examples will make use of this fact.
50 In PyX, the way data is plotted in a graph is defined by a so-called graph
51 style. A couple of standard graph styles are contained in the module
52 `graph.style`. Depending on the data source, PyX chooses a default style. Here,
53 we are taking the data from a file and PyX assumes that the values represent a
54 discrete set of data points. Hence, it chooses the symbol style
55 `graph.style.symbol` to plot the data. To override this default behaviour, you
56 can pass a list of styles as second argument to the `plot` method. For instance,
57 to have PyX drawing a line through the data points, you can use
59 g.plot(graph.data.file("minimal.dat", x=1, y=2), [graph.style.line()])