Netgraph

Documentation

  • Installation & Testing
  • Quickstart
  • Basic Examples
    • Default Design
    • Directed Graphs
    • Weighted Graphs (1)
    • Weighted Graphs (2)
    • Node and Edge Artist Customisation
    • Node and Edge Labels
    • Custom Node Positions and Edge Paths
    • Dot and radial node layouts
    • Circular node layout
    • Community Node Layout / Bundled Edges
    • Curved Edges
    • Graphs with Multiple Components
    • Arc Diagrams
    • Bipartite node layout
    • Highlight paths
    • PyQt example
    • Multi-partite & shell node layouts
    • Node and Edge Legends
    • Hyperlinks
      • Non-interactive figures
      • Interactive figures
    • Geometric node layout
  • Interactive Graphs
  • Animation Examples

API Reference

  • Graph Classes
  • Arc Diagrams
  • Node Layout
  • Edge Layout / Routing
Netgraph
  • Basic Examples
  • Hyperlinks
  • View page source

Note

Click here to download the full example code

Hyperlinks

Non-interactive figures

Netgraph uses matplotlib to draw all figure elements, and matplotib has only limited support for hyperlinks. Specifically, you have to use the SVG backend or export the figure to SVG.

import matplotlib.pyplot as plt

from netgraph import Graph

fig, ax = plt.subplots()
g = Graph([(0, 1)], ax=ax)

g.node_artists[0].set_url("https://www.google.com")
g.edge_artists[(0, 1)].set_url("https://www.stackoverflow.com")

fig.savefig('image.svg')
plot 19 hyperlinks

Opening the image.svg file in a browser of your choice, you can click on the plot elements to open the corresponding web pages.

Interactive figures

Technically, matplotlib does not support hyperlinks when using an interactive backend. However, hyperlink behaviour can easily be emulated using matplotlib pick-events and the python in-built webbrowser module. Note that these hyperlinks are not available when exporting the figure (unless the figure is exported to SVG). Clicking on the PNG image embedded below will hence have no effect.

import webbrowser
import matplotlib.pyplot as plt

from netgraph import Graph

def on_pick(event):
    webbrowser.open(event.artist.get_url())

fig, ax = plt.subplots()

fig.canvas.mpl_connect('pick_event', on_pick)

g = Graph([(0, 1)],
          node_labels={1 : "https://www.github.com"},
          node_label_offset=0.1,
          edge_labels={(0, 1) : "https://matplotlib.org/"},
          ax=ax)

g.node_artists[0].set_picker(True)
g.node_artists[0].set_url("https://www.google.com")

g.edge_artists[(0, 1)].set_picker(10) # increases the pick radius
g.edge_artists[(0, 1)].set_url("https://www.stackoverflow.com")

g.node_label_artists[1].set_picker(True)
g.node_label_artists[1].set_url(g.node_label_artists[1].get_text()) # assumes the label text defines the link target

g.edge_label_artists[(0, 1)].set_picker(True)
g.edge_label_artists[(0, 1)].set_url(g.edge_label_artists[(0, 1)].get_text()) # ditto

plt.show()
plot 19 hyperlinks

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

Download Python source code: plot_19_hyperlinks.py

Download Jupyter notebook: plot_19_hyperlinks.ipynb

Gallery generated by Sphinx-Gallery

Previous Next

© Copyright 2022, Paul Brodersen.

Built with Sphinx using a theme provided by Read the Docs.