python复杂网络库networkx:绘图draw

http://blog.csdn.net/pipisorry/article/details/54291831

networkx使用matplotlib绘制函数

draw(G[, pos, ax, hold])Draw the graph G with Matplotlib.
draw_networkx(G[, pos, arrows, with_labels])Draw the graph G using Matplotlib.
draw_networkx_nodes(G, pos[, nodelist, ...])Draw the nodes of the graph G.
draw_networkx_edges(G, pos[, edgelist, ...])Draw the edges of the graph G.
draw_networkx_labels(G, pos[, labels, ...])Draw node labels on the graph G.
draw_networkx_edge_labels(G, pos[, ...])Draw edge labels.
draw_circular(G, **kwargs)Draw the graph G with a circular layout.
draw_random(G, **kwargs)Draw the graph G with a random layout.
draw_spectral(G, **kwargs)Draw the graph G with a spectral layout.
draw_spring(G, **kwargs)Draw the graph G with a spring layout.
draw_shell(G, **kwargs)Draw networkx graph with shell layout.
draw_graphviz(G[, prog])Draw networkx graph with graphviz layout.

networkx使用Graphviz AGraph (dot)绘制函数

from_agraph(A[, create_using])Return a NetworkX Graph or DiGraph from a PyGraphviz graph.
to_agraph(N)Return a pygraphviz graph from a NetworkX graph N.
write_dot(G, path)Write NetworkX graph G to Graphviz dot format on path.
read_dot(path)Return a NetworkX graph from a dot file on path.
graphviz_layout(G[, prog, root, args])Create node positions for G using Graphviz.

pygraphviz_layout(G[, prog, root, args])

Create node positions for G using Graphviz.

networkx使用Graphviz with pydot绘制函数

from_pydot(P)Return a NetworkX graph from a Pydot graph.
to_pydot(N[, strict])Return a pydot graph from a NetworkX graph N.
write_dot(G, path)Write NetworkX graph G to Graphviz dot format on path.
read_dot(path)Return a NetworkX MultiGraph or MultiDiGraph from a dot file on path.
graphviz_layout(G[, prog, root])Create node positions using Pydot and Graphviz.
pydot_layout(G[, prog, root])Create node positions using Pydot and Graphviz.

图布局Graph Layout

circular_layout(G[, dim, scale, center])Position nodes on a circle.
fruchterman_reingold_layout(G[, dim, k, ...])Position nodes using Fruchterman-Reingold force-directed algorithm.
random_layout(G[, dim, scale, center])Position nodes uniformly at random.
shell_layout(G[, nlist, dim, scale, center])Position nodes in concentric circles.
spring_layout(G[, dim, k, pos, fixed, ...])Position nodes using Fruchterman-Reingold force-directed algorithm.
spectral_layout(G[, dim, weight, scale, center])Position nodes using the eigenvectors of the graph Laplacian.

皮皮blog

networkx绘图示例

示例1:绘制二分图

def draw_bipartite(edges, bi1_node_w_dict, bi2_node_w_dict):
    '''
    :param edges: edges及权重
    :param bi1_node_w_dict: bipartite1中node的大小
    '''
    import networkx as nx
    import numpy as np
    from networkx.algorithms import bipartite
    import matplotlib as mpl
    import matplotlib.pyplot as plt

    ''' node显示label和显示大小设置;edge设置'''
    # node labels的显示名转换(显示名转换;两边node名不能冲突)
    bi1_node_key_dict = {k: 'c' + str(k) + '-' + str(v) for k, v in bi1_node_w_dict.items()}
    bi1_nodes = [bi1_node_key_dict.get(k) for k in bi1_node_w_dict.keys()]
    bi1_node_w = bi1_node_w_dict.values()  # node 显示大小;要和bi1_nodes一一对应不能乱了

    bi2_node_key_dict = {k: 'a' + str(k) + '-' + str(v) for k, v in bi2_node_w_dict.items()}
    bi2_nodes = [bi2_node_key_dict.get(k) for k in bi2_node_w_dict.keys()]
    bi2_node_w = bi2_node_w_dict.values()

    edges = [(bi1_node_key_dict.get(k1), bi2_node_key_dict.get(k2), e_w)
             for k1, k2, e_w in edges]

    print("bi1_nodes:{}".format(bi1_nodes))
    print("bi2_nodes:{}".format(bi2_nodes))
    print("edges:{}".format(edges))

    B = nx.Graph()  # 创建空图
    B.add_nodes_from(bi1_nodes, bipartite=0)
    B.add_nodes_from(bi2_nodes, bipartite=1)
    B.add_weighted_edges_from(edges)
    # print(nx.is_bipartite(B))  # 判断是否是二分图
    # X, Y = bipartite.sets(B) # 不需要这种方式获取

    ''' node weights normalization '''
    # print("bi1_node_w:\n{}".format(bi1_node_w))
    min1, max1 = min(bi1_node_w), max(bi1_node_w)
    bi1_node_w_norm = [(i - min1) / (max1 - min1) for i in bi1_node_w]
    # print("bi1_node_w_norm:\n{}".format(bi1_node_w_norm))

    # print("bi2_node_w:\n{}".format(bi2_node_w))
    min2, max2 = min(bi2_node_w), max(bi2_node_w)
    bi2_node_w_norm = [(i - min2) / (max2 - min2) for i in bi2_node_w]
    # print("bi2_node_w_norm:\n{}".format(bi2_node_w_norm))

    '''绘制node'''
    pos = dict()
    pos.update((n, (1, i)) for i, n in enumerate(bi1_nodes))  # put nodes from X at x=1
    pos.update((n, (2, i)) for i, n in enumerate(bi2_nodes))  # put nodes from X at x=2
    # nx.draw pos parameter: A dictionary with nodes as keys and positions as values
    nx.draw_networkx_nodes(B, pos, nodelist=bi1_nodes, alpha=bi1_node_w_norm,
                           node_size=[5 * i for i in bi1_node_w])  # node_color='slategray',
    nx.draw_networkx_nodes(B, pos, nodelist=bi2_nodes, alpha=bi2_node_w_norm,
                           node_size=[5 * i for i in bi2_node_w])  # node_color='steelblue',
    # 设置nodes中label大小等属性
    nx.draw_networkx_labels(B, pos, font_color='black', font_family='arial',
                            font_size=8)  # label_size_by_freq is not allowed

    '''绘制edges'''
    colors = [B.edges[u, i]['weight'] for u, i in B.edges]
    edges = nx.draw_networkx_edges(B, pos=pos, edge_color=colors,  # edge_cmap=plt.cm.Blues,
                                   width=np.asarray(colors) / 10, edge_vmin=0, alpha=0.9)
    # nx.draw(B, pos=pos, alpha=0.5) #这个会同时绘制node和edges

    '''设置颜色'''
    pc = mpl.collections.PatchCollection([])  # , cmap=plt.cm.Blues)
    pc.set_array(colors)
    plt.colorbar(pc)

    ax = plt.gca()
    ax.set_axis_off()
    # plt.legend()
    plt.show()


actions_cnt = {2: 130, 4: 36, 3: 16, 0: 9, 5: 6}
clusters_cnt = {4: 44, 9: 37, 6: 36, 8: 29, 2: 28, 5: 22, 1: 1}
rudt_cnt_edges = [(1, 2, 1), (2, 2, 22), (2, 4, 3), (2, 3, 3), (4, 2, 22), (4, 4, 10), (4, 0, 7),
                  (4, 3, 5), (5, 2, 22), (6, 2, 22), (6, 4, 7), (6, 5, 4), (6, 3, 3), (8, 2, 21),
                  (8, 4, 6), (8, 3, 2), (9, 2, 20), (9, 4, 10), (9, 3, 3), (9, 0, 2), (9, 5, 2)]

draw_bipartite(rudt_cnt_edges, clusters_cnt, actions_cnt)

 

[Python-NetworkX绘制网络图

[Bipartite graph in NetworkX]

示例2:

def drawGraph(fg, title='weighted_graph'):
    import matplotlib.pyplot as plt
    import networkx as nx

    pos = nx.spring_layout(fg)
    nx.draw_networkx_nodes(fg, pos, node_shape='.', node_size=20)
    nx.draw_networkx_edges(fg, pos)
    nx.draw_networkx_labels(fg, pos)
    nx.draw_networkx_edge_labels(fg, pos, edge_labels=nx.get_edge_attributes(fg, 'weight'))

    plt.savefig(os.path.join(CWD, '../PGT/middlewares/' + title + '.png'))
    plt.show()

其它示例

[Python-NetworkX绘制网络图

from: python复杂网络库networkx:绘图draw_皮皮blog-CSDN博客

ref:官方文档[Drawing graphs]

[networkx reference]

[Drawing]

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值