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 dyngrahplot
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)
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)
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)
plot.close()
# create a new plot
new_plot = DynGraphPlot(new_G)
# update again
new_edges = [(50,20),(51,30), (50,51)]
new_plot.update(new_edges=new_edges)
Usage
DynGraphPlot()
is used to initialize the plot and takes as arguments:
-
G
: NetworkX graph or any object that is valid fornetworkx.Graph()
-
window_box
: Window position and shape, format:[x, y, width, height]
-
draw_options
: Graph visual options fornetworkx.draw()
- arguments are the same as
networkx.draw()
, plusedgecolors
- arguments are the same as
-
initial_layout
: NetworkX layout function (default:networkx.spring_layout
) -
initial_layout_parameters
: Parameters dictionary for initial layout (default:{'k': 0.8}
)
Note:k
is the elasticity parameter for spring layout -
dynamic_layout_parameters
: Parameters dictionary for dynamic layoutpos_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 overlappos_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.2)force_lambda
: temperature decay constant, see paper (default: 0.8)force_iteration_count
: number of layout iterations (default: 50)
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
: thedraw_options
used -
parameters
: thedynamic_layout_parameters
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 tomatplotlib.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for dyngraphplot-0.9.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4521e28541ab9afb0c5853262c384769303eb1d3accc1dbd42b331cf3d39b007 |
|
MD5 | 2243e38133a7ecd936759fb079702273 |
|
BLAKE2b-256 | 495d7789a707d735dc891359cf25693920e6acbe62c16ee3b71e6cd103781abd |