python社交网络图

社交网络图

nx.circular_layout()指定网络图节点的布局方式
nx.draw_networkx_nodes()绘制网络图的节点
nx.draw_networkx_edges()绘制网络的边
nx.draw_networkx_labels()为节点添加标签

import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import os
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
path1=os.getcwd()+'\\'+'红楼梦人物关系1.xlsx'
path2=os.getcwd()+'\\'+'红楼梦人物关系度.xlsx'
Red_df=pd.read_excel(path1)
Gdegree=pd.read_excel(path2)
print(Red_df,Gdegree)
plt.figure(figsize=(8,8))
#生成社交网络图
G=nx.Graph()
for ii in Red_df.index:
    G.add_edge(Red_df.First[ii],
    Red_df.Second[ii],
    weight=Red_df.weight[ii])
#根据权重定义2种边
elarge=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight']>0.4]
esmall=[(u,v) for (u,v,d) in G.edges(data=True) if (d['weight']>0.25)&(d['weight']<=0.4)]
nx.circular_layout(G)##图的布局方式,圆形
pos=nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos,alpha=0.4,node_size=20+Gdegree.degree*15)
#设置边的形式
nx.draw_networkx_edges(G,pos,edgelist=elarge,width=3,alpha=1,edge_color='r')
nx.draw_networkx_edges(G,pos,edgelist=esmall,width=1,alpha=0.8,edge_color='b',style='dashed')
nx.draw_networkx_labels(G,pos,font_size=10)
plt.axis('off')
plt.title("《红楼梦》人物关系")
plt.show()

python社交网络图_第1张图片

你可能感兴趣的:(可视化,python,可视化)