从NetworkX

时间:2016-01-25 23:36:26

标签: javascript python matplotlib networkx sigma.js

将matplotlib与NetworkX结合使用的优点是可以轻松生成PDF,PNG和SVG输出。

import networkx as nx
import matplotlib.pyplot as plt

G1 = nx.Graph()
G.add_nodes_from(["a", "b"])
G.add_edge("a", "b")
nx.draw_networkx(G)
plt.savefig("graph.pdf")
plt.savefig("graph.png")
plt.savefig("graph.svg")

如何修改此代码以生成HTML5 / Javascript Canvas代码?具体的代码,无论多么微不足道,比给定库的生根更有帮助。 Sigmajs似乎很有希望,如果不是因为缺乏文件。

1 个答案:

答案 0 :(得分:0)

实现您想要的最直接的方法是使用Networkx将图表导出为GEXF

G = nx.path_graph(4)
nx.write_gexf(G, "test.gexf")

然后在您的HTML中,您可以使用sigma.js GEXF解析器,如下所示:

<div id="container">
  <style>
    #graph-container {
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      position: absolute;
    }
  </style>
  <div id="graph-container"></div>
</div>
<script>
/**
 * The plugin sigma.parsers.gexf can load and parse the GEXF graph file,
 * and instantiate sigma when the graph is received.
 *
 * The object given as the second parameter is the base of the instance
 * configuration object. The plugin will just add the "graph" key to it
 * before the instanciation.
 */
sigma.parsers.gexf('data/arctic.gexf', { // path to graph here
  container: 'graph-container'
});
</script>

如果你想看一个真实的例子,我建议this blog post at the bottom

免责声明:这是我的博客;)