1. 程式人生 > >python複雜網路庫networkx:繪圖draw

python複雜網路庫networkx:繪圖draw

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 the nodes of the graph G.
Draw the edges of the graph G.
Draw node labels on the graph G.
Draw the graph G with a circular layout.
Draw the graph G with a random layout.
Draw the graph G with a spectral layout.
Draw the graph G with a spring layout.
Draw networkx graph with shell layout.
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.
Return a pygraphviz graph from a NetworkX graph N.
Write NetworkX graph G to Graphviz dot format on path.
Return a NetworkX graph from a dot file on path.
Create node positions for G using Graphviz.
Create node positions for G using Graphviz.

networkx使用Graphviz with pydot繪製函式

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

圖佈局Graph Layout

Position nodes on a circle.
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.

networkx繪圖示例

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()