如何扩展元素列表并绘制它们
How to extend a list of elements and draw them
我在网络中检测到一些社区,我想使该过程尽可能自动化,尤其是在检测到大量社区时。我使用的代码是
from networkx.algorithms.community.modularity_max
import greedy_modularity_communities
c = list(greedy_modularity_communities(G))
检测到 3 个社区。
要查看集群并绘制它们,我通常 运行 以下内容:
community_0 = sorted(c[0])
community_1 = sorted(c[1])
community_2 = sorted(c[2])
并绘制每组节点:
nx.draw_networkx_nodes(G,circ_pos, nodelist=community_0, node_color='g', alpha=0.5)
nx.draw_networkx_nodes(G,circ_pos, nodelist=community_1, node_color='r', alpha=0.5)
nx.draw_networkx_nodes(G,circ_pos, nodelist=community_2, node_color='b', alpha=0.5)
其中 G
是 G = nx.karate_club_graph()
。
我的问题是如何扩展 community_x
的列表,即可以使用 greedy_modularity_communities
检测到的 x 个社区,并绘制它们,在节点列表中迭代添加参数。
使用python eval()
函数并搜索元编程概念。
我为您提供了一些示例代码:
for i in range(1,3):
eval('community_'+str(i)+' = sorted(c['+str(i)+']))'
我会这样做:
from networkx.algorithms.community.modularity_max
import greedy_modularity_communities
# list of detected communities
c = list(greedy_modularity_communities(G))
sortedCommunities = []
# iterate through list of discovered communities. Sort each community and add them to new list.
for community in c:
sortedCommunities.append(sorted(community))
# draw community
# here we are using a different color at each iteration but cycling back to the first color.
colors = ['g', 'r', 'b']
temp_counter = 0
for community in sortedCommunities:
chosenColor = colors[temp_counter%3]
temp_counter += 1
nx.draw_networkx_nodes(G,circ_pos, nodelist=community , node_color=chosenColor , alpha=0.5)
我在网络中检测到一些社区,我想使该过程尽可能自动化,尤其是在检测到大量社区时。我使用的代码是
from networkx.algorithms.community.modularity_max
import greedy_modularity_communities
c = list(greedy_modularity_communities(G))
检测到 3 个社区。 要查看集群并绘制它们,我通常 运行 以下内容:
community_0 = sorted(c[0])
community_1 = sorted(c[1])
community_2 = sorted(c[2])
并绘制每组节点:
nx.draw_networkx_nodes(G,circ_pos, nodelist=community_0, node_color='g', alpha=0.5)
nx.draw_networkx_nodes(G,circ_pos, nodelist=community_1, node_color='r', alpha=0.5)
nx.draw_networkx_nodes(G,circ_pos, nodelist=community_2, node_color='b', alpha=0.5)
其中 G
是 G = nx.karate_club_graph()
。
我的问题是如何扩展 community_x
的列表,即可以使用 greedy_modularity_communities
检测到的 x 个社区,并绘制它们,在节点列表中迭代添加参数。
使用python eval()
函数并搜索元编程概念。
我为您提供了一些示例代码:
for i in range(1,3):
eval('community_'+str(i)+' = sorted(c['+str(i)+']))'
我会这样做:
from networkx.algorithms.community.modularity_max
import greedy_modularity_communities
# list of detected communities
c = list(greedy_modularity_communities(G))
sortedCommunities = []
# iterate through list of discovered communities. Sort each community and add them to new list.
for community in c:
sortedCommunities.append(sorted(community))
# draw community
# here we are using a different color at each iteration but cycling back to the first color.
colors = ['g', 'r', 'b']
temp_counter = 0
for community in sortedCommunities:
chosenColor = colors[temp_counter%3]
temp_counter += 1
nx.draw_networkx_nodes(G,circ_pos, nodelist=community , node_color=chosenColor , alpha=0.5)