SoFunction
Updated on 2024-12-20

Using Python-iGraph how to draw the posting / microblogging friend relationship graph details

preamble

Recently, I encountered some needs in my work, I would like to display the social network specific user's friend relationship by graphical way, I searched the Internet to find a graphical library in this regard networkx, graphviz, etc., and I chose iGraph graphical library after a long time of searching. I've chosen iGraph as my graphical library. Without further ado, let's take a look at the detailed introduction.

Installation of igraph

The installation of igraph in Windows is a little bit troublesome, before trying to use pip and conda to install igraph directly in windows can not be installed, and then found lfd's websiteUnofficial Windows Binaries for Python Extension Packages , which has a lot of python resources and libraries and tools.

Find it at the URL abovepython_igraphGo download the specific python counterpart and whether it's 32-bit or 64-bit, e.g. I downloaded python_igraph-0.7.1.post6-cp35-none-win_amd64. whl

Install the whl file using pip:pip install filename.whl

To avoid errors, after opening the cmd, you have to cd into the extracted directory where you stored the whl file before installing it with pip.

Mapping Friends

and hold the crawled fan nicknames and follower nicknames, respectively.

#coding=utf-8
from igraph import *

count_fans=0   # of fans
count_following=0  # of followers
fans_name=[]   #Fan Nicknames
following=[]   #Follower nicknames
#Open the nickname file under the crawl
with open('','r') as f:  
 lines=()
 for line in lines:
  if (line!=None)&(line!='\n'):
   fans_name.append(line)
   # print fans_name
   count_fans+=1
with open('','r') as c:
 lines=()
 for line in lines:
  if (line!=None)&(line!='\n'):
   (line)
   count_following+=1

g = Graph()   #create
g.add_vertices(3+count_fans+count_following)
g.add_edges([(0,1),(1,2)])

[0]["name"]='Ta's fans'
[1]["name"]='Target Users'
[2]["name"]='Ta's Concerns'
["trunk"] = [True, True]
["main_node"]=[1.5,3,1.5]

for i in range(3,count_fans+3):
 g.add_edges((0,i))
 [i-1]["trunk"]=False
for j in range(count_fans+3,3+count_fans+count_following):
 g.add_edges((2,j))
 [j-1]["trunk"]=False

index=3
for fans in fans_name:
 [index]["name"]=fans
 [index]["main_node"]=False
 index+=1
for name in following:
 [index]["name"]=name
 [index]["main_node"]=False
 index+=1

visual_style = {}
color_dic={1.5:"#cfe6ff",3:"#7299a7",False:"#cfe6ff"}
visual_style["vertex_label_size"]=11
visual_style["vertex_label_dist"]=1
visual_style["vertex_shape"]="circle"
visual_style["vertex_size"] = [7+ 10*int(main_node) for main_node in ["main_node"]]
visual_style["edge_width"] = [1 + 2 * int(trunk) for trunk in ["trunk"]]
visual_style["vertex_color"] =[color_dic[main_node] for main_node in ["main_node"]]
visual_style["vertex_label"] = ["name"]
visual_style["bbox"] = (1000, 1000)
visual_style["margin"] = 150
layout = ("grid_fr")
visual_style["layout"] = layout
plot(g, **visual_style)

The final result is shown in Fig:

The above only demonstrates a user's social graph, if you have the energy, you can try to recursively climb down one layer at a time, imagine the final map is also quite cool.

summarize

Above is the entire content of this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for my support.