社区发现可视化(python3+networkx)

编程入门 行业动态 更新时间:2024-10-10 02:23:33

社区<a href=https://www.elefans.com/category/jswz/34/1771421.html style=发现可视化(python3+networkx)"/>

社区发现可视化(python3+networkx)

网上搜了一些社区发现可视化的代码,发现GitHub上有几个不错的可视化案例,如



这些效果比较好,但是都用了一些其他的包,比如 igraph (安装这个包经常会出问题,我就是因为没有装好就不想用它的画图布局了,然后自己写了这个代码)。

代码里面的配色方案可以参考这篇文章:设计师必备,101个最佳配色方案!

(注:这里面的配色都是16进制的,如果用于PPT等RGB颜色设置的话,需要进行颜色转换,网上有很多颜色码转换工具,如 /)

这是原图
这是采用贪婪算法检测社区之后的可视化效果


代码如下

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from networkx.algorithms import community
import random# science and nature journal '#e3a6a1', '#bc5f6a', '#19b3b1', '#034b61'
# star sky science-fiction '#b2a59f', '#023459', '#1e646e', '#002c2f'
# delicate & shopping & pettyfashion '#b2d6ca', '#fe5858', '#024b40', '#5d353e'colors = ['#fe5858', '#034b61', '#5d353e', '#b2d6ca']   
options = {'font_family': 'serif', 'font_weight': 'semibold', 'font_size': '12', 'font_color': '#ffffff'} 
savefig_path = 'F:/Python/NetworkSci/'def com_postion(size,scale=1, center=(0, 0), dim=2):# generat the postion for each nodes in a communitynum = sizecenter = np.asarray(center)theta = np.linspace(0, 1, num+1)[:-1] * 2 * np.pi    theta = theta.astype(np.float32)pos = np.column_stack([np.cos(theta), np.sin(theta), np.zeros((num, 0))])pos = scale * pos + centerreturn posdef node_postion(one_com,scale=1, center=(0, 0), dim=2):# generat the postion for each nodes in a communitynum = len(one_com)node = list(one_com)center = np.asarray(center)theta = np.linspace(0, 1, num+1)[:-1] * 2 * np.pi   theta = theta.astype(np.float32)pos = np.column_stack([np.cos(theta), np.sin(theta), np.zeros((num, 0))])pos = scale * pos + centerpos = dict(zip(node, pos))return pos# ************************************************************************ #
# part 1--creat a graph
# ************************************************************************ #G = nx.planted_partition_graph(3, 10, 0.7, 0.1)
plt.figure(figsize=(10,10))
pos = nx.shell_layout(G)
nx.draw(G, pos, with_labels=True, **options)
nx.draw_networkx_nodes(G, pos, node_size=600, node_color="#034b61") 
plt.savefig(savefig_path + 'original_network.png', format='png', dpi=500)
plt.show()# ************************************************************************ #
# part 2--community detection
# ************************************************************************ #com = community.greedy_modularity_communities(G) 
num_com = len(com)# find intra_com links
intra_links = {}
for i in range(num_com):intra_links[i] = []for link in nx.edges(G):for i in range(num_com):if (link[0] in com[i]) & (link[1] in com[i]):intra_links[i].append(link)com_center = com_postion(num_com, scale=3)   # print(com_center)
pos = dict()
for val in range(num_com):node_pos = node_postion(com[val], scale=1.3, center=com_center[val])pos.update(node_pos)plt.figure(figsize=(10,10))
nx.draw(G, pos, with_labels=True, edgelist=[], **options)
nx.draw_networkx_edges(G, pos, alpha=0.2, width=0.5)for val in range(num_com): nx.draw_networkx_nodes(G, pos, node_size=600, nodelist=list(com[val]), node_color=colors[val])nx.draw_networkx_edges(G, pos, alpha=0.7, edgelist=intra_links[val], width=1.5)plt.axis("off")
plt.savefig(savefig_path + 'greedy.png', format='png', dpi=500)
plt.show()

更多推荐

社区发现可视化(python3+networkx)

本文发布于:2024-02-07 07:51:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发现   社区   networkx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!