Official ObjectGraph Blog
Friday, July 06, 2007
Graphviz
Today I found something cool for drawing graphs. Its called GraphViz, You can download it from
It can output in various formats including SVG, PNG and GIF. But PNG and GIF dont have aliasing enabled, so they are pretty crappy.
For an example, look here. An image generated using PNG output
http://www.linkulu.com/random/?format=png&numnodes=10&size=5,5
Now look @ an another random graph with SVG output
http://www.linkulu.com/random/?format=svg&numnodes=10&size=5,5
Here is the python code to generate the graphs. I used Django ofcourse
def randomGraph(request): #Defaults for Generating random graphs random.seed() numnodes=5 format="svg" size="2,2" mime="image/svg+xml" if "format" in request.GET: infor=request.GET["format"] if(infor=="png"): mime="image/png" format="png" elif(infor=="gif"): mime="image/gif" format="gif" if "size" in request.GET: size=request.GET["size"] if "numnodes" in request.GET: numnodes=int(request.GET["numnodes"]) dot="""digraph G{ size ="%s"; orientation=portrait; {{{dot}}} }""" % (size) nodes=[] shapes=['ellipse','box','circle','record','triangle','doublecircle'] styles=['bold','dotted','normal'] colors=['cadetblue2','dimgray','dodgerblue1','beige','aliceblue','ghostwhite', 'greenyellow','hotpink4','lightgoldenrod2'] st="" for i in range(numnodes): nodes.append("a"+str(i)); for node in nodes: s1=random.randint(0,len(shapes)-1) c1=random.randint(0,len(colors)-1) st=st+" %s [shape=%s fillcolor=%s style=filled];\n" %(node,shapes[s1],colors[c1]) for i in range(numnodes): r1=random.randint(0,numnodes-1) r2=random.randint(0,numnodes-1) s2=random.randint(0,len(styles)-1) st=st+nodes[r1]+"->"+nodes[r2]+" [style=%s];\n" %(styles[s2]) dot=dot.replace("{{{dot}}}",st) tmpout, filename = mkstemp() pobj=Popen('dot -T%s -o %s' % (format,filename),shell=True,stdin=PIPE,stdout=PIPE) fin = pobj.stdin fout = pobj.stdout fin.write(dot) fin.flush() fin.close() pobj.wait() fout = os.fdopen(tmpout, 'r') jpeg = fout.read() fout.close() os.remove(filename) #jpeg=dot return HttpResponse(content=jpeg, mimetype='%s' % (mime))
Labels: graphviz python
posted by gavi at 4:09 PM
2 Comments:
Hey Gavi: yes, GraphViz is cool. I used it a few years ago to visualize course prerequisites in our curriculum, and see the effects of proposed changes. As for the ugly aliasing in PNG, I think what I did was ask GraphViz to generate it about 4 times larger than I needed, then I used ImageMagick to shrink it — it will apply anti-aliasing and the end result looks much better. Cheers!
By Anonymous, at 10:54 AM
Chris,
I was trying to automate the generation, would it be an efficient process to generate it 4 times larger and shrink?
Another option was to generate SVG and then use conversion software from SVG to png, but i am not sure which will be more efficent.
By gavi, at 9:18 AM
Post a Comment
<< Home