
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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)) |
Related posts:
- PyScraper: The Python Screen Scraper PyScraper is a quick python program i wrote to do...
Related posts brought to you by Yet Another Related Posts Plugin.





















2 Responses
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!
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.