~ubuntu-branches/debian/sid/pyx/sid

« back to all changes in this revision

Viewing changes to examples/graphs/minimal.txt

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2011-05-20 00:13:52 UTC
  • mto: (9.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20110520001352-odcuqpdezuusbbw1
Tags: upstream-0.11.1
ImportĀ upstreamĀ versionĀ 0.11.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Plotting data contained in a file
 
2
 
 
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
 
5
two-column form:
 
6
  # minimal.dat
 
7
  1  2
 
8
  2  3
 
9
  3  8
 
10
  4  13
 
11
  5  18
 
12
  6  21
 
13
 
 
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.
 
18
 
 
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.
 
21
 
 
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`.
 
29
 
 
30
! Note that PyX by default ignores comments starting by a # sign when reading
 
31
in the data from the file.
 
32
 
 
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)
 
37
  # x y
 
38
  1 2
 
39
  ...
 
40
you wouldn't need to label the columns in the `graph.data.file` call at all.
 
41
 
 
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.
 
45
 
 
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.
 
49
 
 
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
 
58
 
 
59
    g.plot(graph.data.file("minimal.dat", x=1, y=2), [graph.style.line()])