python 网络分成社区的漂亮情节

kpbwa7wx  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(108)

我有一个城市的图表,这些城市被划分成不同年份的模块化社区。每个社区分配的节点位于一个单独的数据集根据年份(例如,2000年的community2000等等)。图G包含如下节点:

{'Node': {0: 'albany', 1: 'almaty', 2: 'amsterdam'}}

每个ODE都有很多边(对于奥尔巴尼来说低于一些边):

{'Source': {0: 'albany',
  1: 'albany',
  2: 'albany',
  3: 'albany',
  4: 'albany',
  5: 'albany',
  6: 'albany',
  7: 'albany',
  8: 'albany',
  9: 'albany'},
 'Target': {0: 'almaty',
  1: 'amsterdam',
  2: 'ankara',
  3: 'athens',
  4: 'atlanta',
  5: 'auckland',
  6: 'austin',
  7: 'bangalore',
  8: 'bangkok',
  9: 'barcelona'}}

每个节点都在G.nodes[node]['Community']中属性一个社区,这是通过我拥有的可用社区 Dataframe (community_df)完成的,如下所示:

for node in G.nodes():
        city = node
        community = community_df.loc[community_df['city'] == city, 'cluster'+community_filename[-4:]].values[0]
        G.nodes[node]['Community'] = community

我目前使用的绘制网络图的代码如下:

import matplotlib.pyplot as plt

# Create a dictionary to map community labels to unique integers
community_labels = {}
next_community_label = 0

# Assign a unique integer label to each community
for node in G.nodes():
    community = G.nodes[node]['Community']
    if community not in community_labels:
        community_labels[community] = next_community_label
        next_community_label += 1

# Create a dictionary to map cluster labels to unique integers within each community
cluster_labels = {}

# Remove self-loops
G.remove_edges_from(nx.selfloop_edges(G))

# Draw the network graph
pos = nx.spring_layout(G, k=0.3)  # Layout algorithm for node positioning
plt.figure(figsize=(12, 8))

for community in community_labels.values():
    nodes = [node for node, attr in G.nodes(data=True) if attr['Community'] == community]
    
    # Assign cluster labels within each community
    for node in nodes:
        cluster = community_df.loc[community_df['city'] == node, 'cluster' + community_filename[-4:]].values[0]
        cluster_labels[node] = cluster
    
    node_colors = [cluster_labels[node] for node in nodes]
    
    nx.draw_networkx_nodes(G, pos, nodelist=nodes, node_size=200, node_color=node_colors, cmap='viridis')
    nx.draw_networkx_edges(G, pos, edgelist=G.edges, alpha=0.1, width=0.5)
    nx.draw_networkx_labels(G, pos, font_size=8, font_color='black', labels={node: node for node in nodes})

plt.axis('off')
plt.title("Network of Cities Divided into Communities and Clusters")
plt.tight_layout()
plt.show()

这样的代码的结果是不可读的,并且没有像我希望的那样划分为社区:

我想要的结果是这样的(以城市名称作为节点的标签):

jgwigjjp

jgwigjjp1#

这里有一些东西可以让你开始:
其基本思想是使用nx.spring_layout生成位置,但要为每个社区单独执行。

#dummy graph
G = nx.from_numpy_matrix(np.random.rand(100,100)>0.9)
for node in G.nodes:
    G.nodes[node]['community'] = np.random.randint(0,4)

# organise communities:
communities = {}
for node, data in G.nodes(data=True):
    c = data['community']
    if c not in communities:
        communities[c] = [node]
    else:
        communities[c].append(node)
        
# build individual positions for each community:
# add offsets to move clusters around:
offsets = [[0,0], [0,5], [5,0], [5,5]]
pos = {}
for offset, nodes in zip(offsets, communities.values()):
    community_position = nx.spring_layout(G.subgraph(nodes), center=offset)
    pos = {**pos, **community_position}
    
nx.draw_networkx(G, pos=pos)

相关问题