2014-09-22 270 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陰謀交互式節目。

相關問題