PyX can be used for data and function plotting. At present
x-y-graphs and x-y-z-graphs are supported only. However, the component
architecture of the graph system described in section
4.2 allows for additional graph geometries while
reusing most of the existing components.
Creating a graph splits into two basic steps. First you have to create
a graph instance. The most simple form would look like:
from pyx import *
g = graph.graphxy(width=8)
The graph instance g created in this example can then be used
to actually plot something into the graph. Suppose you have some data
in a file graph.dat you want to plot. The content of the file
could look like:
1 2
2 3
3 8
4 13
5 18
6 21
To plot these data into the graph g you must perform:
g.plot(graph.data.file("graph.dat", x=1, y=2))
The method plot() takes the data to be plotted and optionally
a list of graph styles to be used to plot the data. When no styles are
provided, a default style defined by the data instance is used. For
data read from a file by an instance of graph.data.file, the
default are symbols. When instantiating graph.data.file, you
not only specify the file name, but also a mapping from columns to
axis names and other information the styles might make use of
(e.g. data for error bars to be used by the errorbar style).
While the graph is already created by that, we still need to perform a
write of the result into a file. Since the graph instance is a canvas,
we can just call its writeEPSfile() method.
The result graph.eps is shown in figure 4.1.
Figure:
A minimalistic plot for the data from file graph.dat.
|
|
Instead of plotting data from a file, other data source are available
as well. For example function data is created and placed into
plot() by the following line:
g.plot(graph.data.function("y(x)=x**2"))
You can plot different data in a single graph by calling
plot() several times before writeEPSfile() or
writePDFfile(). Note that a calling plot() will fail
once a graph was forced to ``finish'' itself. This happens
automatically, when the graph is written to a file. Thus it is not an
option to call plot() after writeEPSfile() or
writePDFfile(). The topic of the finalization of a graph is
addressed in more detail in section 4.3. As you can see
in figure 4.2, a function is plotted as a line by
default.
Figure 4.2:
Plotting data from a file together with a function.
|
|
While the axes ranges got adjusted automatically in the previous
example, they might be fixed by keyword options in axes constructors.
Plotting only a function will need such a setting at least in the
variable coordinate. The following code also shows how to set a
logathmic axis in y-direction:
from pyx import *
g = graph.graphxy(width=8, x=graph.axis.linear(min=-5, max=5),
y=graph.axis.logarithmic())
g.plot(graph.data.function("y(x)=exp(x)"))
g.writeEPSfile("graph3")
g.writePDFfile("graph3")
The result is shown in figure 4.3.
Figure 4.3:
Plotting a function for a given axis range and use a
logarithmic y-axis.
|
|