Data visualisation is a powerful tool to get insight from complex data, particularly from the vast collections of information available online. One example of this kind of data would be social networking data. In the last few years, we've all seen the explosion of social networking sites such as MySpace, Facebook, Hi5, LastfFM and many more. Many of them make available their 'social connectedness' data, giving interesting insights into how poeple relate.
Informatics in Action
In this example, we map the social connectedness of data from LastFM, an online social networking site which recommends music based on the preferences of like-minded people. Within complex relationship networks, visualization helps us to investigate the characteristics of social interactions: for example, whether a user has few friends who each have lots of friends, or he/she is someone with lots of friends, each of whom just have few friends.
1importos2os.environ['TMP']='c:/sandbox/temp/'34frommod_pythonimport*5importsys6importurllib7importxml.dom.minidom8fromarrayimport*9fromxml.dom.minidomimportNode10frompylabimport*11fromnetworkximport*12fromnetworkx.generators.atlasimport*13fromminghiimport*14frommingimport*151617nodeSize={}18textSize={}19lineSize={}20myCirc={}2122proxies={'http':'http://wwwproxy.unimelb.edu.au:8000'}# unimelb proxy23#proxies = {} # use this if you want to by pass the proxy
2425deffindfriends(user):26# talk to lastfm web services
27remotexml=urllib.urlopen("http://ws.audioscrobbler.com/1.0/user/"+user+"/friends.xml",proxies=proxies)28friends=[]29try:30doc=xml.dom.minidom.parse(remotexml)31except:32returnfriends33i=034fornodeindoc.getElementsByTagName("user"):35friends.append(node.getAttribute("username"))36i+=137ifi>12:38returnfriends3940returnfriends4142defbuildgraph(user,outputName):43G=XGraph(name="lastfmgraph",multiedges=True,selfloops=False)4445# build the graph
46friendlist=findfriends(user)47nodeSize[user]=8048textSize[user]=24049lineSize[user]=4050foruinfriendlist:51G.add_edge(user,u)52friendlist2=findfriends(u)53nodeSize[u]=5054textSize[u]=15055lineSize[u]=2056foru2infriendlist2:57G.add_edge(u,u2)58ifnodeSize.has_key(u2)==False:59nodeSize[u2]=2060textSize[u2]=6061lineSize[u2]=106263# layouting algorithm to generate the position for all nodes
64pos=spring_layout(G,iterations=15)6566# initialize flash file
67Ming_setScale(1)68m=SWFMovie()69m.setDimension(6400,4800)70m.setRate(12.0)7172minx=10073maxx=-10074miny=10075maxy=-1007677# preparing the scale for flash file
78forvinG:79(px,py)=pos[v]80ifpx<minx:81minx=px82ifpx>maxx:83maxx=px84ifpy<miny:85miny=py86ifpy>maxy:87maxy=py88centerx=(minx+maxx)/289centery=(miny+maxy)/290lengthx=maxx-minx91lengthy=maxy-miny92xscale=6400/lengthx93yscale=4800/lengthy94ifxscale>yscale:95scale=yscale96else:97scale=xscale98scale=0.9*scale99100newpos={}101102# draw nodes
103forvinG:104(px,py)=pos[v]105px=(px-centerx)*scale+3200106py=-(py-centery)*scale+2400107newpos[v]=(px,py)108size=nodeSize[v]*3109myCirc=drawCircle(px,py,size,200,0,0,80)110m.add(myCirc)111112# draw lines
113foru,v,winG.edges():114(x1,y1)=newpos[u]115(x2,y2)=newpos[v]116iflineSize[u]>lineSize[v]:117lsize=lineSize[u]118else:119lsize=lineSize[v]120m.add(drawLine(x1,y1,x2,y2,lsize,0,100,100,100))121122# draw labels
123forvinG:124(px,py)=newpos[v]125size=textSize[v]126myTxt=drawText(str(v),"m:/devbox/apache.root/common/Bitstream Vera Sans.fdb",size,0,0,0,255)127firstText=m.add(myTxt)128firstText.moveTo(px,py)129130m.nextFrame()131#save flash file
132m.save(outputName)133134deflocal(req):# run locally, and save in local directory135buildgraph("washedrind","c:/temp/lastfmnet.swf")136137defindex(req):# run in web server138fs=util.FieldStorage(req)139req.content_type="text/html"140req.write('<html><body>')141142iffs.getfirst("user")==None:143req.write('put user="someone"')144else:145buildgraph(fs["user"],"m:/devbox/apache.root/showcase/output/lastfmnet.swf")146147# send an html file that contain the flash file
148req.write('<object width="100%" height="100%">')149req.write('<param name="movie" value="/showcase/output/lastfmnet.swf">')150req.write('<embed src="/showcase/output/lastfmnet.swf" width="100%" height="100%">')151req.write('</embed></object>')152req.write("<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2808341-10");
pageTracker._trackPageview();
</script></body></html>")153154# web/local switcher
155if__name__=="__main__":156local(sys.stdout)