Want to graphically display the social network specific user's friend relationship, the Internet to find a bit of this aspect of the graphics library has networkx, Graphviz and so on, looking for a long time I chose igraph this graphics library.
Igraph is a little bit cumbersome to install under Windows, see: https://my.oschina.net/stu51/blog/335455
Fans.txt and Concern.txt each saved a fan nickname and a followers nickname.
#Coding=utf-8 fromIgraphImport*Count_fans=0#Number of FansCount_following=0#number of followersFans_name=[]#Fan NicknameFollowing=[]#Attention Person Nickname#to open a crawl-removed nickname fileWith open ('Fans.txt','R') as F:lines=F.readlines () forLineinchlines:if(Line!=none) & (line!='\ n'): Fans_name.append (line)#Print Fans_nameCount_fans+=1With Open ('Concern.txt','R') as C:lines=C.readlines () forLineinchlines:if(Line!=none) & (line!='\ n'): Following.append (line) count_following+=1g= Graph ()#CreateG.add_vertices (3+count_fans+count_following) g.add_edges ([(0,1), ()]) g.vs[0]["name"]='TA's fans'g.vs[1]["name"]='Target User'g.vs[2]["name"]='TA's concern'g.es["Trunk"] =[True, true]g.vs["Main_node"]=[1.5,3,1.5] forIinchRange (3,count_fans+3): G.add_edges ((0,i)) G.es[i-1]["Trunk"]=False forJinchRange (count_fans+3,3+count_fans+count_following): G.add_edges ((2, J)) G.es[j-1]["Trunk"]=Falseindex=3 forFansinchfans_name:g.vs[index]["name"]=fans g.vs[index]["Main_node"]=False Index+=1 forNameinchfollowing:g.vs[index]["name"]=name g.vs[index]["Main_node"]=False Index+=1Visual_style={}color_dic={1.5:"#8DB6CD", 3:"#969696", False:"#97FFFF"}visual_style["vertex_label_size"]=20visual_style["vertex_label_dist"]=2visual_style["Vertex_shape"]="Circle"visual_style["vertex_size"] = [15+ 10*int (Main_node) forMain_nodeinchg.vs["Main_node"]]visual_style["Edge_width"] = [1 + 2 * INT (trunk) forTrunkinchg.es["Trunk"]]visual_style["Vertex_color"] =[color_dic[main_node] forMain_nodeinchg.vs["Main_node"]]visual_style["Vertex_label"] = g.vs["name"]visual_style["bbox"] = (1000, 1000) visual_style["margin"] = 100Layout= G.layout ("KK") visual_style["Layout"] =Layoutplot (g,**visual_style)
Final result
It feels a little stiff ╮(╯▽╰)╭
First sauce, one more time to beautify the next
Draw a friend diagram using Python-igraph