|
|
淡定的小蝌蚪 · 如何将一个大小为4200行×256列的二维矩 ...· 1 年前 · |
|
|
玩篮球的跑步鞋 · 如何在PowerShell中连接字符串和变量 ...· 2 年前 · |
|
|
沉着的西红柿 · angular8 ...· 2 年前 · |
|
|
眉毛粗的风衣 · 【无标题】-CSDN博客· 2 年前 · |
似乎在networkx中应该有一种方法来导出json图形格式,但我没有看到它。我认为使用nx.to_dict_of_dicts()应该很容易做到这一点,但需要一些操作。有人知道一个简单而优雅的解决方案吗?
节点和边是否有足够的信息?如果是这样,您可以编写自己的函数:
json.dumps(dict(nodes=graph.nodes(), edges=graph.edges()))
下面是我刚刚使用的一种JSON方法,以及读取结果的代码。它保存节点和边属性,以防您需要。
import simplejson as json
import networkx as nx
G = nx.DiGraph()
# add nodes, edges, etc to G ...
def save(G, fname):
json.dump(dict(nodes=[[n, G.node[n]] for n in G.nodes()],
edges=[[u, v, G.edge[u][v]] for u,v in G.edges()]),
open(fname, 'w'), indent=2)
def load(fname):
G = nx.DiGraph()
d = json.load(open(fname))
G.add_nodes_from(d['nodes'])
G.add_edges_from(d['edges'])
return G
此 documentation 包含完整描述
下面是一个简单的例子:
import networkx as nx
from networkx.readwrite import json_graph
DG = nx.DiGraph()
DG.add_edge('a', 'b')
print json_graph.dumps(DG)
您还可以查看有关将物理功能添加到图形可视化中的 Javascript/SVG/D3 示例。
|
|
眉毛粗的风衣 · 【无标题】-CSDN博客 2 年前 |