我想手动勾画出一个图形拓扑(字面意思是指向和点击),然后使用该草图使用Python的Graph
库创建一个NetworkX
对象,最好(尽管不是绝对必要的) )在Windows环境中。
在Inkscape中绘制图形可能会有效,然后解析xml
文件以创建NetworkX
Graph
;但这看起来很笨重。有一个更好的方法吗?
答案 0 :(得分:2)
我维护了一个交互式图表绘图库,它有一些支持这样做。它被称为netgraph
,你可以在github找到它或通过pip安装它。该项目最初是作为networkx绘图工具的一个分支(这不足以满足我的需求),因此API非常相似。
import numpy as np
import matplotlib.pyplot as plt
import networkx
from netgraph import InteractivelyConstructDestroyGraph
# initialise figure
fig, ax = plt.subplots()
# set size of axis to draw on
ax.set(xlim=[-2, 2], ylim=[-2, 2])
# initialise graph;
# netgraph supports many graph input formats but an empty graph is not one of them;
# hence we pass in a single edge in an edge list format
g = InteractivelyConstructDestroyGraph([(0, 1)], draw_arrows=True, ax=ax)
plt.show()
# manipulate graph:
# Pressing 'A' will add a node to the graph.
# Pressing 'D' will remove a selected node.
# Pressing 'a' will add edges between all selected nodes.
# Pressing 'd' will remove edges between all selected nodes.
# Pressing 'r' will reverse the direction of edges between all selected nodes.
# Nodes can be selected and moved using the mouse.
# get current edge list
edge_list = g.edge_list
# get current node positions, which is a dict int node : (float x, float y) (same format as in networkx)
node_positions = g.node_positions
# create graph in networkx
G = networkx.Graph()
G.add_edges_from(edge_list)
# reproduce plot in networkx
networkx.draw(G, pos=node_positions)
plt.show()
代码库的这一部分仍然相当......实验性的(当我看到你的问题时,我只是合并并上传了相关的部分)。因此,任何错误报告都特别受欢迎。 ;-)
答案 1 :(得分:0)
关注NetworkX's docs,提到“Cytoscape,Gephi,Graphviz和LaTeX排版,PGF / TikZ”。
我正在尝试Gephi,这似乎与我想要的非常接近,虽然有点滞后。