#NetworkX提供了基本的绘图功能,但是更强调的是对图的分析,而不是图的可视化。在以后的功能中,
#图的可视化可能被移除,或只提供一个附加的包
#著名的绘图工具是:Cytoscape, Gephi, Graphviz and, for LaTeXtypesetting, PGF/TikZ.
#使用这些工具,需要将Networx的图转换成这些工具能够读的格式,列如:
#Cytoscape能够读GraphML格式的图,所以用networkx.writegraphml(G)可能是一个恰当的选择
Matplotlib
用Matplotlib绘图
matplotlib
http://matplotlib.org/
pygraphviz
http://pygraphviz.github.io/
In [6]:
# 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.
In [1]:
importnetworkx as nx
importmatplotlib.pyplot as plt
In [4]:
G = nx.complete_graph(5)
draw(G)
In [15]:
nx.draw_spectral(G)
plt.show()
In [39]:
G.nodes()
Out[39]:
[0, 1, 2,3, 4]
nx.draw_networkx
In [6]:
pos=nx.spring_layout(G)
nx.draw_networkx(G, pos, arrows=True, with_labels=True,nodelist=G.nodes(),\ #基本参数
node_color=[1,2,3,4,5],node_shape="*",node_size=350,alpha=0.5,\ #结点参数,alpha是透明度
width=2,edge_color=[1,2,3,4,5,6,7,8,9,0],style='dashed',\ #边参数(solid|dashed|dotted,dashdot)
labels={0:'A',1:'B',2:'C',3:'D',4:'E'},font_size=20,font_weight='bold',\
font_family="Times NewRoman",\
label=['MyFun']
)
plt.show()
. . .
In [7]:
pos=nx.spring_layout(G)
nx.draw_networkx(G, pos, arrows=True, with_labels=True,nodelist=G.nodes(),\
node_color=[1,2,3,4,5],node_shape="*",node_size=350,alpha=0.5,\
width=2,edge_color=[1,2,3,4,5,6,7,8,9,0],style='dashed',\
labels={0:'A',1:'B',2:'C',3:'D',4:'E'},font_size=20,font_weight='bold',\
font_family="Times NewRoman",\
label=['MyFun']
)
plt.show()
In [13]:
G = nx.complete_graph(5)
pos = nx.spring_layout(G) #注意不能在下面函数中调用nx.spring_layout,而应该先定义一个pos,大家共用
nodes=nx.draw_networkx_nodes(G,pos,node_color=[1,2,3,4,5],label='AAAA')
edges=nx.draw_networkx_edges(G,pos)
edges=nx.draw_networkx_labels(G,pos)
#labels=nx.draw_networkx_labels(G,pos=nx.spring_layout(G))
plt.show()
draw_networkx_edge_labels
In [9]:
# G.add_edge('10','9',weight=1, label='I')
# edge_labels =dict([((u, v), d['label'])
# for u, v, d in G.edges(data=True)])
In [62]:
G.edges(data=True)
Out[62]:
[(0, 1,{}),
(0, 2, {}),
(0, 3, {}),
(0, 4, {}),
(1, 2, {}),
(1, 3, {}),
(1, 4, {}),
(2, 3, {}),
(2, 4, {}),
(3, 4, {})]
In [11]:
# draw_networkx_edge_labels(G, pos, edge_labels=None,label_pos=0.5, font_size=10, font_color='k', \
# font_family='sans-serif', font_weight='normal', \
# alpha=1.0,bbox=None, ax=None, rotate=True, **kwds)
# n=nx.draw(G,pos)
#e=nx.draw_networkx_edge_labels(G,pos,edges_labes={G.edges()[0]:"0-1"})
# plt.show()
Graphviz AGraph (dot)
In [7]:
# Graphviz AGraph
# Interface to pygraphviz AGraph class.
AGraph的格式转换与读写
In [3]:
A=nx.nx_agraph.to_agraph(G)
H=nx.nx_agraph.from_agraph(A) #注意 此处需要安装pygraphviz包
In [52]:
# from_agraph(A[, create_using]) Return a NetworkX Graph or DiGraph from aPyGraphviz graph.
# to_agraph(N) Return a pygraphviz graph from a NetworkX graph N.
# write_dot(G, path) Write NetworkX graph G to Graphviz dotformat 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.
Graph Layout
In [53]:
# 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-Reingoldforce-directed algorithm.
# spectral_layout(G[, dim, weight, scale,center]) Position nodes using theeigenvectors of the graph Laplacian.
In [ ]:
# spring_layout(G, dim=2, k=None, pos=None,fixed=None, iterations=50, weight='weight', scale=1.0, center=None)
# Position nodes using Fruchterman-Reingoldforce-directed algorithm.
# Parameters:
# G (NetworkX graph) –
# dim (int) – Dimension of layout
# k (float (default=None)) – Optimal distance betweennodes. If None the distance is set to 1/sqrt(n) where n is the number of nodes.Increase this value to move nodes farther apart.
# pos (dict or None optional (default=None)) –Initial positions for nodes as a dictionary with node as keys and values as alist or tuple. If None, then use random initial positions.
# fixed (list or None optional (default=None)) –Nodes to keep fixed at initial position. If any nodes are fixed, the scale andcenter features are not used.
# iterations (int optional (default=50)) – Number ofiterations of spring-force relaxation
# weight (string or None optional (default='weight'))– The edge attribute that holds the numerical value used for the effectivespring constant. If None, edge weights are 1.
# scale (float (default=1.0)) – Scale factor forpositions. The nodes are positioned in a box of size scale in each dim centeredat center.
# center (array-like (default scale/2 in each dim)) –Coordinate around which to center the layout.
# Returns:
# A dictionary of positions keyed by node
# Return type:
# dict