PyX — Examples

The following examples are part of the PyX release 0.16. For each PyX example the source code and the corresponding output are shown. You can take a guided tour through all examples by the prev and next links on the upper right. All the examples and their descriptions are included in the source distribution of PyX in the examples subdirectory.

Some paragraphs (like the one you're just reading) are marked with a dangerous bend sign. Some people will recognize this classification from the TeXbook by D.E. Knuth and we use it at the PyX example pages for the same purpose. The bend marks parts of the description, which require some experiences with PyX. On the other hand, those parts can savely be ignored by PyX beginners.

There are even some paragraphs marked as doubly dangerous. The explanations given in those paragraphs provide some deeper insights of what's really going on, but are not important for the average PyX user.

0.1 KB
15.0 KB
21.0 KB
11.7 KB
8.8 KB

Hello, world!

hello.png
from pyx import *

c = canvas.canvas()
c.text(0, 0, "Hello, world!")
c.stroke(path.line(0, 0, 2, 0))
c.writeEPSfile()
c.writePDFfile()
c.writeSVGfile()

Description

As it is good practice to say "Hello, world!" in the first example, let's discuss how to do that in PyX.

At first we import the PyX modules. Most PyX programs will start with a line like that. In order to produce some output we create a canvas instance c. Such an instance provides some useful methods to output some text at a certain position and for directly stroking a path, for which we use a line instance from the path module here. Once this is done, we write an EPS file and a PDF file containing all items inserted into the canvas instance. Since we did not pass a filename as argument to the write methods, the resulting files will just be named as the Python script - minus the extension, of course.

This rather minimal example is quite useful for debugging your setup. In case of problems with your installation, you can add a pyxinfo() call immediately after the import. PyX will then write various information to stderr, which is quite helpful for yourself or when asking for help on the mailing list.

Further examples