I wrote jgraph to visualize graphs in 3D purely out of curiosity. I couldn't find any 3D force-directed graph libraries when I wrote it, so this happened.
It can be used with the notebook to interactively view graph data. Some graphs just look nicer in 3D. To use, send it Python objects. A simple data structure is the adjacency list.
In [1]:
import jgraph
jgraph.draw([(1, 2), (2, 3), (3, 4), (4, 1), (4, 5), (5, 2)])
That works, but the graph is boring. More complex graph visualizations use node size and color to show multiple dimensions. To do this with jgraph, you can use a more expressive Python data structure. Each node takes three optional parameters: color, size, and location.
In [2]:
graph = {
'nodes': {
'ross': {'color': 0xffaaaa, 'size': 2.0},
'joey': {'size': 0.5},
'chandler': {'color': 0x2222ff, 'size': 1.25},
'phoebe': {'color': 0x22ff22},
'rachel': {},
'monica': {},
'jack': {},
'judy': {},
},
'edges': [
{'source': 'chandler', 'target': 'ross'},
{'source': 'monica', 'target': 'ross'},
{'source': 'ross', 'target': 'rachel', 'size': 3, 'color': 0xffaaaa},
{'source': 'ross', 'target': 'joey'},
{'source': 'ross', 'target': 'phoebe'},
{'source': 'ross', 'target': 'judy'},
{'source': 'monica', 'target': 'rachel'},
{'source': 'rachel', 'target': 'jack'},
{'source': 'chandler', 'target': 'phoebe'}
]
}
jgraph.draw(graph)
The draw
method can take python objects, strings, or files.
In [3]:
jgraph.draw('miserables.json', directed=False)
Because the input is a pure Python data structure, you can programmatically create and edit graphs without learning a new API. This lets you mess around with graph data structures and algorithms without thinking about library-specific semantics.
In [4]:
number_of_steps = 5
b_tree = [(1, 2), (1, 3)]
index = 3
for _ in range(number_of_steps):
leaves = [edge[1] for edge in b_tree if all(edge[1] != other_edge[0] for other_edge in b_tree)]
for leaf in leaves:
for __ in range(2):
index += 1
b_tree.append((leaf, index))
jgraph.draw(b_tree, shader="lambert", default_node_color=0x383294, z=200, size=(800, 600))
You may have noticed some extra arguments used in that last example. There are options to change default colors and sizing, renderers, and more. For a full breakdown, read the docs.
In [5]:
help(jgraph.draw)
You may also want more control over generating the force-directed graph from an adjacency list. For that use jgraph.generate
.
In [6]:
help(jgraph.generate)