PyX – Python graphics package

PyX is a Python package for the creation of PostScript and PDF files. It combines an abstraction of the PostScript drawing model with a TeX/LaTeX interface. Complex tasks like 2d and 3d plots in publication-ready quality are built out of these primitives.

Here is the type of things you can do:

Naturally, the code is a bit hairy:


from math import pi, cos
from pyx import *
from pyx.deco import barrow, earrow
from pyx.style import linewidth, linestyle
from pyx.graph import graphxy
from pyx.graph.axis import linear
from pyx.graph.axis.painter import regular
from pyx.graph.style import line
from pyx.graph.data import function
mypainter = regular(basepathattrs=[earrow.normal], titlepos=1)
def mycos(x): return -cos(x)+.10*x
g = graphxy(height=5, x2=None, y2=None,
x=linear(min=-2.5*pi, max=3.3*pi, parter=None,
painter=mypainter, title=r"$\\delta\\phi$"),
y=linear(min=-2.3, max=2, painter=None))
g.plot(function("y(x)=mycos(x)", context=locals()),
[line(lineattrs=[linewidth.Thick])])
g.finish()
x1, y1 = g.pos(-pi+.1, mycos(-pi+.1))
x2, y2 = g.pos(-.1, mycos(-.1))
x3, y3 = g.pos(pi+.1, mycos(pi+.1))
g.stroke(path.line(x1-.5, y1, x1+.5, y1), [linestyle.dashed])
g.stroke(path.line(x1-.5, y3, x3+.5, y3), [linestyle.dashed])
g.stroke(path.line(x2-.5, y2, x3+.5, y2), [linestyle.dashed])
g.stroke(path.line(x1, y1, x1, y3), [barrow.normal, earrow.normal])
g.stroke(path.line(x3, y2, x3, y3), [barrow.normal, earrow.normal])
g.text(x1+.2, 0.5*(y1+y3), r"$2\\pi\\gamma k\\Omega$", [text.vshift.middlezero])
g.text(x1-.6, y1-.1, r"$E_{\\rm b}$", [text.halign.right])
g.text(x3+.15, y2+.20, r"$2J_k(\\varepsilon/\\Omega)+\\pi\\gamma k\\Omega$")
g.writeEPSfile("washboard")
g.writePDFfile("washboard")

(Source: 0xDE)

2 thoughts on “PyX – Python graphics package”

  1. That’s a very good looking graph. At a minimum this could be used as a better looking gd lib. Probably could be used to generate images for titles where flash is used today (some designers are using flash to generate fancy titles and sections on web pages).

    Hmmm, this is almost like gnuplot with Python as its control language. Quite a few possibilities here…

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax