Search code examples
python-3.xgraphicsnodesnetworkxvispy

No example available for `vispy.visuals.GraphVisual`. My code just shows blank screen


I tried googling and piecing together an example from many sources. Here is what I've got:

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()

The output is a blank (black) screen, with nothing else displayed.

My goal is to display a simple network graph using VisPy. Have you gotten this to work and could you post your code / relevant code?


Solution

  • It looks like your on_draw method is missing the most important part which is to call the draw method of the visual. See the graph example from the vispy repository:

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

    Here are the methods from that example:

    
        @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()