How to Use Python-iGraph to draw the friend relationship diagram of the post/microblog, and use python-igraph to draw

Source: Internet
Author: User

How to Use Python-iGraph to draw the friend relationship diagram of the post/microblog, and use python-igraph to draw

Preface

I have encountered some requirements in my recent work. I want to display the friend relationship of a specific user on the social network in a graphical way, And I found networkx and graphviz on the Internet, after searching for a long time, I chose the graphics library iGraph. I won't talk much about it below. Let's take a look at the detailed introduction.

Install igraph

Igraph installation in Windows is a little troublesome. Previously, I tried to install igraph in windows without pip or conda. Later I found the lfd website Unofficial Windows Binaries for Python Extension Packages, there are many python resources, libraries, and tools.

In the above URL, find python_igraph to download the python version and whether it is 32-bit or 64-bit. For example, I downloaded python_igraph 255.0.7.1.post6 255.cp35 without using win_amd64.whl.

Use pip to install the whl file:Pip install file name. whl

To avoid errors, after you open cmd, you need to run the cd to enter the decompressed directory of the whl file you saved and install it with pip.

Draw friend relationship diagram

Fans.txt and follow.txt respectively Save the names of followers and followers who have crawled.

# Coding = utf-8from igraph import * count_fans = 0 # followers count count_following = 0 # followers fans_name = [] # followers nickname following = [] # followers nickname # Open the nickname for crawling file with open('fans.txt ', 'R') as f: lines = f. readlines () for line in lines: if (line! = None) & (line! = '\ N'): fans_name.append (line) # print fans_name count_fans + = 1 with open('follodomaintxt', 'R') as c: lines = c. readlines () for line in lines: if (line! = None) & (line! = '\ N'): following. append (line) count_following + = 1g = Graph () # create g. add_vertices (3 + count_fans + count_following) g. add_edges ([(0, 1), (1, 2)]) g. vs [0] ["name"] = 'ta fans 'g. vs [1] ["name"] = 'target user' g. vs [2] ["name"] = 'ta follow 'g. es ["trunk"] = [True, True] g. vs ["main_node"] = [1.5, 3, 1.5] for I in range (3, count_fans + 3): g. add_edges (0, I) g. es [I-1] ["trunk"] = Falsefor j in range (count_fans + 3,3 + count_fans + count_following): g. add_edges (2, j) g. es [J-1] ["trunk"] = Falseindex = 3for fans in fans_name: g. vs [index] ["name"] = fans g. vs [index] ["main_node"] = False index + = 1for name in following: g. vs [index] ["name"] = name g. vs [index] ["main_node"] = False index + = 1visual_style = {} color_dic = {1.5: "# cfe6ff", 3: "#7299a7", False: "# cfe6ff"} visual_style ["vertex_label_size"] = 11visual_style ["external"] = 1visual_style ["vertex_shape"] = "circle" visual_style ["vertex_size"] = [7 + 10 * int (main_node) for main_node in g. vs ["main_node"] visual_style ["edge_width"] = [1 + 2 * int (trunk) for trunk in g. es ["trunk"] visual_style ["vertex_color"] = [color_dic [main_node] for main_node in g. vs ["main_node"] visual_style ["vertex_label"] = g. vs ["name"] visual_style ["bbox"] = (1000,100 0) visual_style ["margin"] = 150 layout = g. layout ("grid_fr") visual_style ["layout"] = layoutplot (g, ** visual_style)

Final Result

The above shows only the social relationship diagram of a user. If you have the energy, you can try to crawl layer by layer recursively. it is cool to imagine the final figure.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.