
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/scene/graph.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_scene_graph.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_scene_graph.py:


Visualize NetworkX Graph
========================

This example demonstrates how to visualise a NetworkX graph using a VisPy
Graph.

.. GENERATED FROM PYTHON SOURCE LINES 14-46



.. image-sg:: /gallery/scene/images/sphx_glr_graph_001.gif
   :alt: graph
   :srcset: /gallery/scene/images/sphx_glr_graph_001.gif
   :class: sphx-glr-single-img





.. code-block:: Python


    import sys

    import networkx as nx

    from vispy import app, scene
    from vispy.visuals.graphs import layouts


    canvas = scene.SceneCanvas(title='Simple NetworkX Graph', size=(600, 600),
                               bgcolor='white', show=True)
    view = canvas.central_widget.add_view('panzoom')

    graph = nx.adjacency_matrix(
        nx.fast_gnp_random_graph(500, 0.005, directed=True))
    layout = layouts.get_layout('force_directed', iterations=100)

    visual = scene.visuals.Graph(
        graph, layout=layout, line_color='black', arrow_type="stealth",
        arrow_size=30, node_symbol="disc", node_size=20,
        face_color=(1, 0, 0, 0.2), border_width=0.0, animate=True, directed=False,
        parent=view.scene)


    @canvas.events.draw.connect
    def on_draw(event):
        if not visual.animate_layout():
            canvas.update()

    if __name__ == '__main__':
        if sys.flags.interactive != 1:
            app.run()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 2.087 seconds)


.. _sphx_glr_download_gallery_scene_graph.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: graph.ipynb <graph.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: graph.py <graph.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: graph.zip <graph.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
