-1

我尝试使用谷歌搜索并拼凑来自许多来源的示例。这是我所拥有的:

import numpy as np
from vispy import app
from vispy import visuals
from vispy.visuals.transforms import STTransform
import networkx as nx

class Canvas(app.Canvas):
    def __init__(self, **kwargs):
        super().__init__(title="Simple NetworkX Graph", keys="interactive", size=(600, 600))

        graph = nx.path_graph(8)
        #graph = nx.adjacency_matrix(
                #nx.fast_gnp_random_graph(500, 0.005, directed=True))
        layout = nx.layout.circular_layout
        self.matrix = nx.adjacency_matrix(graph)
            
        self.visual = visuals.GraphVisual(
                nx.adjacency_matrix(graph), 
                layout=layout, 
                line_color='white', arrow_type="angle_30",
                arrow_size=30, node_symbol="disc", node_size=20,
                face_color=(1, 0, 0, 0.5), border_width=0.0, animate=True,
                directed=True)
    
        self.visual.transform = STTransform((1, 1), (20, 20))
        self.show()

    #def on_resize(self, event):
        #set_viewport(0, 0, *event.physical_size)

    def on_draw(self, event):
        clear(color=True, depth=True)

if __name__ == '__main__':
    c = Canvas(title="Graph")
    app.run()

输出是一个空白(黑色)屏幕,没有其他任何显示。

我的目标是使用 VisPy 显示一个简单的网络图。你有没有让这个工作,你能发布你的代码/相关代码吗?

4

2 回答 2

2

看起来您的on_draw方法缺少最重要的部分,即调用视觉对象的绘制方法。请参阅 vispy 存储库中的图形示例:

https://github.com/vispy/vispy/blob/master/examples/basics/visuals/graph.py

以下是该示例中的方法:


    @property
    def visual_size(self):
        return self.physical_size[0] - 40, self.physical_size[1] - 40

    def on_resize(self, event):
        self.visual.transform.scale = self.visual_size
        vp = (0, 0, self.physical_size[0], self.physical_size[1])
        self.context.set_viewport(*vp)
        self.visual.transforms.configure(canvas=self, viewport=vp)

    def on_draw(self, event):
        self.context.clear('white')
        self.visual.draw()
        if not self.visual.animate_layout():
            self.update()
于 2020-09-30T13:01:59.617 回答
0

一种选择是不使用 VisPy,而是使用matplotlib

import matplotlib.pyplot as plt
import networkx as nx

G = nx.path_graph(8)
nx.draw(G)
plt.show()

上图非常快速地在一个窗口中显示。

另一个替代库是grave

import networkx as nx
import matplotlib.pyplot as plt
from grave import plot_network

# Generate a networkx graph
graph = nx.powerlaw_cluster_graph(50, 1, .2)

# Plot it
plot_network(graph)

这似乎允许某些形式的图形交互(使用鼠标)。

于 2020-09-21T04:11:37.117 回答