Skip to main content

A module for plotting dynamic force-directed graphs

Project description

dyngraphplot

dyngraphplot is a Python module for the drawing of dynamic force-directed graphs that change over time. It is based on the algorithm by Frishman, Tal in the paper: Online Dynamic Graph Drawing

This is a simplified, non-parallel version of that algorithm without the partitioning steps, but this way it's easier to implement and use, while performance should still be sufficient for smaller-size graphs.

The implementation heavily relies on the matplotlib and networkx modules.

Installation

To install dyngraphplot, do:

$ pip install dyngraphplot

Simple Example

Then, to initialize and plot a graph:

import networkx as nx
from dyngraphplot import DynGraphPlot

# create a random graph and plot it
G = nx.fast_gnp_random_graph(50, 0.1)
plot = DynGraphPlot(G)

# pause until "Enter" is pressed, as mode is non-blocking by default
input()

And afterwards, to update the graph:

# update nodes and edges in the graph
new_nodes = [50,51]
new_edges = [(50,20),(51,30), (50,51)]
plot.update(new_nodes, new_edges)

# wait before exiting
input()

Note that update returns the updated networkx.Graph object, so you can do:

# update a plot, get result and close plot
new_nodes = [50,51]
new_G = plot.update(new_nodes)
new_layout = plot.layout
plot.close()

Usage

DynGraphPlot() is used to initialize the plot and takes as arguments:

  • G: NetworkX graph or any object that is valid for networkx.Graph()

  • mode: Drawing mode for the plot, options are:

    • 'non-blocking': Show the plot and update it without blocking running proccess (default)
    • 'blocking': Show the plot, block running proccess, must close plot to resume (matplotlib bug doesn't apply in this mode, window is responsive)
    • 'save': Save the dynamic graph as a sequence of files in a directory
    • 'hidden': Don't plot the graph at all (useful for getting layout x,y of nodes)
  • plot_box: Plot position and shape, format: [x, y, width, height]

  • save_dir: Directory to save the plot sequence in 'save' mode

  • save_name: Filename for the plot files in 'save' mode (default: graph.png) individual graphs will be saved as graph_0.png, graph_1.png, etc.

  • draw_options: Graph visual options for networkx.draw()
    arguments are the same as networkx.draw(), plus:

    • edgecolors: Set color of node borders
    • edge_labels: Edge labels in a dictionary keyed by edge two-tuple of text labels
    • edge_label_attr: Name of edge attribute to be used as edge label, edges that have that attribute are drawn with it as label
  • initial_layout: NetworkX layout function (default: networkx.spring_layout)

  • initial_layout_params: Parameters dictionary for initial layout (default: {'k': 0.8})
    Note: k is the elasticity parameter for spring layout

  • dynamic_layout_params: Parameters dictionary for dynamic layout

    • pos_radius: # radius for placing unconnected new nodes (default: 0.618)
    • pos_angle: # angle step for placing unconnected new nodes (default: 3)
      Note: this shouldn't be multiple of pi or nodes will often overlap
    • pos_score_same: positioning confidence score for unmoved nodes (default: 1)
    • pos_score_2: positioning confidence score for nodes with 2+ placed neighbors (default: 0.25)
    • pos_score_1: positioning confidence score for nodes with 1 placed neighbor (default: 0.1)
    • pos_score_0: positioning confidence score for nodes without placed neighbors (default: 0)
    • pin_a: pinning rigidity, see paper (default: 0.6)
    • pin_k: pinning cutoff, see paper (default: 0.5)
    • pin_weight: initial pinning weight, see paper (default: 0.35)
    • force_K: optimal geometric distance, see paper (default: 0.1)
    • force_lambda: temperature decay constant, see paper (default: 0.9)
    • force_iteration_count: number of layout iterations (default: 50)
    • force_dampen: dampening factor for force application (default: 0.1)

DynGraphPlot.update() is used to update the plot and takes as arguments:

  • new_nodes: Iterable containing nodes added in this update

  • new_edges: Iterable containing edges added in this update

  • rmv_nodes: Iterable containing nodes removed in this update

  • rmv_edges: Iterable containing edges removed in this update


DynGraphPlot.close() is used to close the plot


A DynGraphPlot object also has a number of useful accessible properties:

  • G: NetworkX object with the current graph

  • options: the draw_options used

  • params: the dynamic_layout_params used

  • layout: Dictionary with [x,y] position of every node

  • figure: The matplotlib figure being drawn

  • ax: The axis of the matplotlib figure

Notes

  • GUI Freeze: A persistent bug in the matplotlib module causes the plot window to freeze when in interactive (non-blocking) mode. Unforunately, as dyngraphplot needs to draw dynamic graphs, the interactive mode must be used and to allow figure updating. This means that the plot windows generated by dyngraphplot can't be interacted with and therefore can't be resized, zoomed in or saved. I'm exploring possible solutions to this using a threading / multiproccessing architecture, but it may be unstable, back-end specific or impossible.

  • Window position and size: Because of the previously GUI freeze problem, dyngraphplot includes a (hopefully) OS and back-end agnostic way to set the plot window position and size, since it won't be possible to resize it later.

  • Node borders: Normally networkx.draw() fails to pass the edgecolors attribute to matplotlib.scatter, which means nodes are drawn without borders. A workaround is included in dyngraphplot that manually does this, although this causes a very minor visual bug where labels of overlapping nodes may be drawn with incorrect depths.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dyngraphplot-1.2.2.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

dyngraphplot-1.2.2-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file dyngraphplot-1.2.2.tar.gz.

File metadata

  • Download URL: dyngraphplot-1.2.2.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for dyngraphplot-1.2.2.tar.gz
Algorithm Hash digest
SHA256 46d0662559dad6f1f962f6fba935e43aef23c02324943f6d230774bc5259770a
MD5 49d0255ab4b4bf734efd7e085a5d6e24
BLAKE2b-256 b758dad4e9e3a2ca558549986a1d0c0ed7cdf9d8d0755d58685b28f0d0ee4ff9

See more details on using hashes here.

File details

Details for the file dyngraphplot-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: dyngraphplot-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for dyngraphplot-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e72c8a48d2e6d44a42b9f6aef76f08146317c3dbc3e6ebf00a814fb129366554
MD5 e2c20a2b4c8b5ffd68b86df73f0df6b3
BLAKE2b-256 3fe2a32be05c2511d3ba0b54ae675659d73c8de5f6de4817c6d6228345fd0804

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page