每日总结-第八天-networkx

2020-04-08  本文已影响0人  SamiraG

networkx库

G = nx.Graph() # new graph
G.add_node(1)
G.add_node(1, attr = '[FUNC]')  #增加节点属性
G.add_node(2)
G.add_edge(1, 2)
G.add_edge(1, 2, attr1 = 'this_is_a_edge_attribute') #增加边属性
nx.draw(G)
plt.show()

get subgraph

G = nx.read_gml("call.gml",label="id")
def n_neighbor(node, graph, hop=2, show=False):
    ego_graph = nx.ego_graph(graph, node, radius=hop, undirected=True)
    nodes = ego_graph.nodes      
    edges = ego_graph.edges
    if show:
        import matplotlib.pyplot as plt
        # plot to check
        color = ['r' if n==node else 'b' for n in nodes]
        nx.draw(ego_graph, node_color=color); plt.show()
    return nodes, edges
n_neighbor(5,G,show=True)

TODO

上一篇下一篇

猜你喜欢

热点阅读