我正在寻找一个用于Python的交互式图形库。
我所说的"graph“是指由一组顶点连接的一组节点(不是x-y轴上的值的图,也不是像素的网格)。
所谓“交互式”,我的意思是我可以拖放周围的节点,我需要能够单击节点/顶点,并让库将节点/顶点传递给我的回调,回调可能会添加/删除节点/顶点或显示信息(我不能在启动时加载完整的图形,因为数据集太大/太复杂;相反,我将根据用户输入仅加载必要的数据切片)。
我所说的Python指的是编程语言Python,图形库应该有CPython绑定。我有Python 2.7和Python 3.1,但如果需要可以降级到2.6。这种语言要求是因为我使用的dataset只有Python绑定。
图形库必须支持directed graph,并且能够自动布局节点。我需要在节点上贴上标签。
优选地,布局算法应该将相邻节点放置在彼此附近。在我4岁的笔记本电脑上,它应该能够合理地处理100-1000个节点和大约300-4000个顶点(我通常从100个节点开始,但这个数字可能会根据用户的输入而扩展)。最好它应该是一个没有太多依赖项的库(也许除了Gnome)。最好是开源的。
我已经使用Tkinter Canvas编写了一个简单的程序原型,但我需要一个更严肃的图形库来扩展该程序。我看过graphviz和matplotlib,但显然它们只用于处理静态图形,显然需要大量的工作才能进行交互操作(如果我错了,请纠正我,我只是简单地看了一下它们)。我还尝试过将图形生成为SVG文件并使用Inkscape查看它,但它太慢了,占用了太多内存,而且由于顶点数量太多,它变得乱七八糟。
发布于 2015-01-17 00:35:33
我也有同样的问题。最后,我认为nodebox opengl似乎做到了这一点。不要试图在以下链接中使用图形库
http://nodebox.net/code/index.php/Graph
使用nodebox opengl。它不工作,图形库仅与mac OSX nodebox兼容。但在任何情况下这都是可以的,因为你不需要它。
例如,请参阅以下问题:
Adding label to an edge of a graph in nodebox opnegl
它显示了适用于我的示例代码,代码可以修改,这样单击节点不仅可以移动节点,还可以修改图形。
只需删除
label = "Placeholder"
从代码中提取出来,并且它起作用了。
编辑:
我在这里放了一些更详细的示例代码:Nodebox open GL Graph, size function not recognized. (Ubuntu)
发布于 2020-07-21 17:21:34
我思考并尝试了这个问题中给出的所有解决方案,最终得到了以下解决方案。
我认为最好的可伸缩解决方案是将Matplotlib的交互模式与networkx结合使用。下面的代码段解释了如何为鼠标单击显示数据点的注释。由于我们使用的是Networkx,因此该解决方案的可扩展性比预期的要高得多。
import networkx as nx
import matplotlib.pyplot as plt
import nx_altair as nxa
from pylab import *
class AnnoteFinder: # thanks to http://www.scipy.org/Cookbook/Matplotlib/Interactive_Plotting
"""
callback for matplotlib to visit a node (display an annotation) when points are clicked on. The
point which is closest to the click and within xtol and ytol is identified.
"""
def __init__(self, xdata, ydata, annotes, axis=None, xtol=None, ytol=None):
self.data = list(zip(xdata, ydata, annotes))
if xtol is None: xtol = ((max(xdata) - min(xdata))/float(len(xdata)))/2
if ytol is None: ytol = ((max(ydata) - min(ydata))/float(len(ydata)))/2
self.xtol = xtol
self.ytol = ytol
if axis is None: axis = gca()
self.axis= axis
self.drawnAnnotations = {}
self.links = []
def __call__(self, event):
if event.inaxes:
clickX = event.xdata
clickY = event.ydata
print(dir(event),event.key)
if self.axis is None or self.axis==event.inaxes:
annotes = []
smallest_x_dist = float('inf')
smallest_y_dist = float('inf')
for x,y,a in self.data:
if abs(clickX-x)<=smallest_x_dist and abs(clickY-y)<=smallest_y_dist :
dx, dy = x - clickX, y - clickY
annotes.append((dx*dx+dy*dy,x,y, a) )
smallest_x_dist=abs(clickX-x)
smallest_y_dist=abs(clickY-y)
print(annotes,'annotate')
# if clickX-self.xtol < x < clickX+self.xtol and clickY-self.ytol < y < clickY+self.ytol :
# dx,dy=x-clickX,y-clickY
# annotes.append((dx*dx+dy*dy,x,y, a) )
print(annotes,clickX,clickY,self.xtol,self.ytol )
if annotes:
annotes.sort() # to select the nearest node
distance, x, y, annote = annotes[0]
self.drawAnnote(event.inaxes, x, y, annote)
def drawAnnote(self, axis, x, y, annote):
if (x, y) in self.drawnAnnotations:
markers = self.drawnAnnotations[(x, y)]
for m in markers:
m.set_visible(not m.get_visible())
self.axis.figure.canvas.draw()
else:
t = axis.text(x, y, "%s" % (annote), )
m = axis.scatter([x], [y], marker='d', c='r', zorder=100)
self.drawnAnnotations[(x, y)] = (t, m)
self.axis.figure.canvas.draw()
df = pd.DataFrame('LOAD YOUR DATA')
# Build your graph
G = nx.from_pandas_edgelist(df, 'from', 'to')
pos = nx.spring_layout(G,k=0.1, iterations=20) # the layout gives us the nodes position x,y,annotes=[],[],[] for key in pos:
x, y, annotes = [], [], []
for key in pos:
d = pos[key]
annotes.append(key)
x.append(d[0])
y.append(d[1])
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.set_title('select nodes to navigate there')
nx.draw(G, pos, font_size=6,node_color='#A0CBE2', edge_color='#BB0000', width=0.1,
node_size=2,with_labels=True)
af = AnnoteFinder(x, y, annotes)
connect('button_press_event', af)
show()
https://stackoverflow.com/questions/5759878
复制相似问题