Networkx 中 Louvain 分区的可视化
Visualization of Louvain partitions in Networkx
请帮我改变可视化Louvain聚类算法的结果。
我从网站上拿了代码
https://github.com/taynaud/python-louvain
我可以重写代码,让每个簇都有自己的形状(圆形、三角形、方形……)吗?
import community as community_louvain
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import networkx as nx
# load the karate club graph
G = nx.karate_club_graph()
# compute the best partition
partition = community_louvain.best_partition(G)
# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40,
cmap=cmap, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.5)
plt.show()
不幸的是nx.draw_networkx_nodes
does not accept an iterable of shapes, so you'll have to loop over the nodes and plot them individually. Also, we'll have to index the generated cmap
, otherwise, the single valued community values will get mapped to the same initial cmap color. For the possible shapes I'm just replicating the string of available shapes mentioned in the docs并根据分区号对其进行索引:
# load the karate club graph
G = nx.karate_club_graph()
# compute the best partition
partition = community_louvain.best_partition(G)
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
shapes = 'so^>v<dph8'
plt.figure(figsize=(12,8))
# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_edges(G, pos, alpha=0.5)
for node, color in partition.items():
nx.draw_networkx_nodes(G, pos, [node], node_size=100,
node_color=[cmap.colors[color]],
node_shape=shapes[color])
请帮我改变可视化Louvain聚类算法的结果。 我从网站上拿了代码 https://github.com/taynaud/python-louvain 我可以重写代码,让每个簇都有自己的形状(圆形、三角形、方形……)吗?
import community as community_louvain
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import networkx as nx
# load the karate club graph
G = nx.karate_club_graph()
# compute the best partition
partition = community_louvain.best_partition(G)
# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40,
cmap=cmap, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.5)
plt.show()
不幸的是nx.draw_networkx_nodes
does not accept an iterable of shapes, so you'll have to loop over the nodes and plot them individually. Also, we'll have to index the generated cmap
, otherwise, the single valued community values will get mapped to the same initial cmap color. For the possible shapes I'm just replicating the string of available shapes mentioned in the docs并根据分区号对其进行索引:
# load the karate club graph
G = nx.karate_club_graph()
# compute the best partition
partition = community_louvain.best_partition(G)
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
shapes = 'so^>v<dph8'
plt.figure(figsize=(12,8))
# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_edges(G, pos, alpha=0.5)
for node, color in partition.items():
nx.draw_networkx_nodes(G, pos, [node], node_size=100,
node_color=[cmap.colors[color]],
node_shape=shapes[color])