## [PyX-user] Graph drawing stuff

 [PyX-user] Graph drawing stuff From: Magnus Lie Hetland - 2005-09-22 20:22:04 ```OK, this is probably pretty useless, but -- since there seems to be others interested in drawing graphs, here is some old code I managed to dig out. I can't get it to work with the current PyX, though: from pyx import * text.set(mode='latex') text.preamble(r'\usepackage{mathpple}') unit.set(uscale=2) class Node: def __init__(self, canvas, x, y, label, radius=unit.t_cm(0.25)): self.box = canvas.text(x, y, label, text.halign.center, text.valign.middle) canvas.stroke(path.circle(x, y, radius)) self.radius = radius self.x = x self.y = y # XXX Bogus null-box for use with connectors: self.center = box.rect(x, y, 0, 0) class Edge: def __init__(self, canvas, source, target, *args, **kwds): connclass = kwds.get('connclass', connector.line) self.source = source self.target = target self.conn = connclass(source.center, target.center, # XXX Add half the width of the circle # path: boxdists=[source.radius, target.radius]) canvas.stroke(self.conn, *args) def drawNodes(canvas): A = Node(c, 0, 1, '\$a\$') B = Node(c, 0, 0, '\$b\$') T = Node(c, 1, 1, '\$t\$') C = Node(c, 1, 0, '\$c\$') return A, B, T, C c = canvas.canvas() A, B, T, C = drawNodes(c) for X, Y in [[A, T], [A, C], [B, C]]: Edge(c, X, Y, [style.linestyle.dashed, #deco.earrow.normal(style.linestyle.solid)) deco.earrow.normal([deco.stroked.clear])]) c.writetofile('graph') -- Magnus Lie Hetland "The early bird may get the worm but the second http://hetland.org mouse gets the cheese." -- Willie Nelson ```

 [PyX-user] Graph drawing stuff From: Magnus Lie Hetland - 2005-09-22 20:22:04 ```OK, this is probably pretty useless, but -- since there seems to be others interested in drawing graphs, here is some old code I managed to dig out. I can't get it to work with the current PyX, though: from pyx import * text.set(mode='latex') text.preamble(r'\usepackage{mathpple}') unit.set(uscale=2) class Node: def __init__(self, canvas, x, y, label, radius=unit.t_cm(0.25)): self.box = canvas.text(x, y, label, text.halign.center, text.valign.middle) canvas.stroke(path.circle(x, y, radius)) self.radius = radius self.x = x self.y = y # XXX Bogus null-box for use with connectors: self.center = box.rect(x, y, 0, 0) class Edge: def __init__(self, canvas, source, target, *args, **kwds): connclass = kwds.get('connclass', connector.line) self.source = source self.target = target self.conn = connclass(source.center, target.center, # XXX Add half the width of the circle # path: boxdists=[source.radius, target.radius]) canvas.stroke(self.conn, *args) def drawNodes(canvas): A = Node(c, 0, 1, '\$a\$') B = Node(c, 0, 0, '\$b\$') T = Node(c, 1, 1, '\$t\$') C = Node(c, 1, 0, '\$c\$') return A, B, T, C c = canvas.canvas() A, B, T, C = drawNodes(c) for X, Y in [[A, T], [A, C], [B, C]]: Edge(c, X, Y, [style.linestyle.dashed, #deco.earrow.normal(style.linestyle.solid)) deco.earrow.normal([deco.stroked.clear])]) c.writetofile('graph') -- Magnus Lie Hetland "The early bird may get the worm but the second http://hetland.org mouse gets the cheese." -- Willie Nelson ```
 Re: [PyX-user] Graph drawing stuff From: Alan G Isaac - 2005-09-22 21:38:53 ```Thanks Magnus, Things I think PyX doesn't like: Node doesn't put the text attributes in a list, and the zero-area rectangle appears unwelcome. First impressions, Alan Isaac ```
 Re[2]: [PyX-user] Graph drawing stuff From: Alan G Isaac - 2005-09-22 21:59:12 ```OK, this basically works. Thanks, Alan from pyx import * text.reset() text.set(mode=3D'latex') #unit.set(uscale=3D2) unit.set(defaultunit=3D'cm') class Node: =09def __init__(self, canvas, x, y, label, radius=3D0.25*unit.t_cm): =09=09self.box =3D canvas.text(x, y, label,[ text.halign.center, =09=09=09=09=09=09 text.valign.middle]) =09=09canvas.stroke(path.circle(x, y, radius)) =09=09self.radius =3D radius =09=09self.x =3D x =09=09self.y =3D y =09=09# XXX Bogus null-box for use with connectors: =09=09# self.center =3D box.rect(x, y, 0, 0) class Edge: =09def __init__(self, canvas, source, target, *args, **kwds): =09=09connclass =3D kwds.get('connclass', connector.line) =09=09self.source =3D source =09=09self.target =3D target =09=09self.conn =3D connclass(source.box, target.box, =09=09=09=09=09=09=09 # XXX Add half the width of the circle =09=09=09=09=09=09=09 # path: =09=09=09=09=09=09=09 boxdists=3D[source.radius, target.radius]) =09=09canvas.stroke(self.conn, *args) def drawNodes(canvas): =09A =3D Node(c, 0, 1, '\$a\$') =09B =3D Node(c, 0, 0, '\$b\$') =09T =3D Node(c, 1, 1, '\$t\$') =09C =3D Node(c, 1, 0, '\$c\$') =09return A, B, T, C c =3D canvas.canvas() A, B, T, C =3D drawNodes(c) for X, Y in [[A, T], [A, C], [B, C]]: =09Edge(c, X, Y, [style.linestyle.dashed, =09=09 #deco.earrow.normal(style.linestyle.solid)) =09=09 deco.earrow.normal([deco.stroked.clear])]) c.writeEPSfile('c:/temp/temp.eps') ```
 Re: [PyX-user] Graph drawing stuff From: Magnus Lie Hetland - 2005-09-22 23:48:40 ```Alan G Isaac : > > Thanks Magnus, > > Things I think PyX doesn't like: > Node doesn't put the text attributes in a list, > and the zero-area rectangle appears unwelcome. Sure. It worked at one time, but PyX changed. -- Magnus Lie Hetland "The early bird may get the worm but the second http://hetland.org mouse gets the cheese." -- Willie Nelson ```