2014-09-22 273 views

回答

0

Here is the documentation您正在寻找,有很多解决方案。我可以补充说,如果没有人应该读或修改创建的文件(这只是一种存储格式),你可以使用pickle。如果您需要更通用的格式,因为该图将用于其他工具,您可能更喜欢graphML或Json。

实施例:

>>> cube = nx.hypercube_graph(2) 
>>> nx.write_gpickle(cube,"cube.gpickle") 
>>> readCube = nx.read_gpickle("cube.gpickle") 
>>> cube.edge 
{(0, 1): {(0, 0): {}, (1, 1): {}}, (1, 0): {(0, 0): {}, (1, 1): {}}, (0, 0): {(0, 1): {}, (1, 0): {}}, (1, 1): {(0, 1): {}, (1, 0): {}}} 
>>> readCube.edge 
{(0, 1): {(0, 0): {}, (1, 1): {}}, (1, 0): {(0, 0): {}, (1, 1): {}}, (0, 0): {(0, 1): {}, (1, 0): {}}, (1, 1): {(0, 1): {}, (1, 0): {}}} 
6

是的! Networkx将绘制成matplotlib图形,此后可以使用所有matplotlibs API,包括保存文件(选择格式和dpi)。

>>> import networkx as nx 
>>> import matplotlib 
>>> matplotlib.use("Agg") 
>>> import matplotlib.pyplot as plt 
>>> g = nx.Graph() 
>>> g.add_edge(1,2) 
>>> f = plt.figure() 
>>> nx.draw(g, ax=f.add_subplot(111)) 
>>> f.savefig("graph.png") 

线matplotlib.use("Agg")是可选的,但它是适当的,从来没有要显示matplotlib阴谋交互式节目。

相关问题