简体   繁体   English

如何根据图的模块化改变networkx图中节点的颜色

[英]How to change color of nodes in networkx graph based on graph's modularity

Currently, i have a NetworkX graph that changes color and size of nodes based on degree centrality, but I am looking to instead of changing color based on degree centrality, I am looking to change the color of the nodes based on modularity, preferably using label propagation in the calculation of the modularity.目前,我有一个 NetworkX 图,它可以根据度中心性更改节点的颜色和大小,但我希望不是根据度中心性更改颜色,而是希望根据模块性更改节点的颜色,最好使用 label模块化计算中的传播。

I've tried changing the color the same way I did on the code that changes according to degree centrality, but only getting errors as the degree centrality had multiple values and the modularity was one single value.我尝试以与根据度数中心性更改的代码相同的方式更改颜色,但由于度数中心性具有多个值并且模块化是一个值,因此只会出现错误。

The expected result is to have the color of the nodes change depending on modularity instead of degree centrality, while keeping the size of the nodes based on degree centrality.预期的结果是使节点的颜色根据模块性而不是度中心性而改变,同时根据度中心性保持节点的大小。

the CSV file used in this project is available here: https://www.mediafire.com/file/q0kziy9h251fcjf/nutrients.csv/file此项目中使用的 CSV 文件可在此处获得: https://www.mediafire.com/file/q0kziy9h251fcjf/nutrients.csv/file

Here is the code for the project这是项目的代码

import networkx as nx
import matplotlib.pyplot as plt
import networkx.algorithms.community as nx_com
import numpy as np

# create graph from data
with open("nutrients.csv", "r") as f:
    G = nx.parse_edgelist(f.readlines(), delimiter=",")

# centrality
deg_centrality = nx.degree_centrality(G)
centrality = np.fromiter(deg_centrality.values(), float)

# modularity
mod = nx_com.modularity(G, nx_com.label_propagation_communities(G))

# plot
pos = nx.spring_layout(G)
nx.draw(G, pos, node_color=centrality, node_size=centrality*2e3)
nx.draw_networkx_labels(G, pos)
plt.show()

I fixed the problem.我解决了这个问题。 here is the answer这是答案

import networkx as nx
import matplotlib.pyplot as plt
import networkx.algorithms.community as nx_com
import numpy as np
import community as community_louvain

# create graph from data
with open("nutrients.csv", "r") as f:
    G = nx.parse_edgelist(f.readlines(), delimiter=",")

# centrality
deg_centrality = nx.degree_centrality(G)
centrality = np.fromiter(deg_centrality.values(), float)

# modularity
label = community_louvain.best_partition(G)
mod = community_louvain.modularity(label, G)
values = [label.get(node) for node in G.nodes()]


# plot
pos = nx.spring_layout(G)
nx.draw(G, pos, node_color=values, node_size=centrality*2e3)
nx.draw_networkx_labels(G, pos)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python networkx中的图形模块化 - Graph modularity in python networkx 如何根据节点的邻接关系使用NetworkX绘制图形 - How to draw a graph with NetworkX based on adjacency of nodes 如何使网络图中节点的颜色从Networkx的中心到边缘顺序变化 - How to make color of nodes in the network graph change sequentially from the center to the edge in Networkx 如何使用networkx图根据中间节点的属性值找到2个节点之间的路径? - How to find a path between 2 nodes based on in-between node's attribute's value using networkx graph? 如何在Networkx图形图中确定所选节点的特定颜色和大小 - How to determine specific color and size of chosen nodes in Networkx graph plot 如何在networkx中的网格上绘制节点 - How to graph nodes on a grid in networkx 基于度中心性度量更改networkx中图形的颜色和大小 - Change Color & Size of graph in networkx based on degree centrality measure 如何根据注释属性对networkx python图中的节点进行分组? - How to group nodes in a networkx python graph based on note attributes? 有没有办法根据它们的连接程度将 Networkx 图中的节点分组在一起? - Is there a way to group nodes in a Networkx graph together based on how connected they are? 如何更改networkX中二部图的节点和边缘的颜色? - How to change colours of nodes and edges of bipartite graph in networkX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM