有多种方法可以根据属性选择节点。这是使用get_node_attributes 和列表理解来获取子集的方法。然后绘图函数接受 nodelist 参数。
应该很容易扩展到更广泛的条件集或根据这种方法修改每个子集的外观以满足您的需求
import networkx as nx
# define a graph, some nodes with a "Type" attribute, some without.
G = nx.Graph()
G.add_nodes_from([1,2,3], Type='MASTER')
G.add_nodes_from([4,5], Type='DOC')
G.add_nodes_from([6])
# extract nodes with specific setting of the attribute
master_nodes = [n for (n,ty) in \
nx.get_node_attributes(G,'Type').iteritems() if ty == 'MASTER']
doc_nodes = [n for (n,ty) in \
nx.get_node_attributes(G,'Type').iteritems() if ty == 'DOC']
# and find all the remaining nodes.
other_nodes = list(set(G.nodes()) - set(master_nodes) - set(doc_nodes))
# now draw them in subsets using the `nodelist` arg
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, nodelist=master_nodes, \
node_color='red', node_shape='o')
nx.draw_networkx_nodes(G, pos, nodelist=doc_nodes, \
node_color='blue', node_shape='o')
nx.draw_networkx_nodes(G, pos, nodelist=other_nodes, \
node_color='purple', node_shape='s')