In [1]:
import numpy as np
from bqplot import *
from bqplot.marks import Graph
from ipywidgets import Layout
Nodes and Links should be supplied to the Graph mark.
Node attributes
Attribute | Type | Description | Default |
---|---|---|---|
label | str | node label | mandatory attribute |
label_display | {center, outside, none} | label display options | center |
shape | {circle, ellipse, rect} | node shape | circle |
shape_attrs | dict | node SVG attributes | {'r': 15} |
Link Attributes
Attribute | Type | Description | Default |
---|---|---|---|
source | int | source node index | mandatory attribute |
target | int | target node index | mandatory attribute |
value | float | value of the link. Use np.nan if you do not want a link | - |
Link data can also be supplied using a link_matrix which is a numpy array of shape (n, n) where n is the number of nodes in the graph
Link Data can be passed in through 3 data attributes -
In [2]:
fig_layout = Layout(width='960px', height='500px')
In [3]:
node_data = list('ABCDEFG')
link_data = [{'source': s, 'target': t} for s, t in np.random.randint(0, 7, (10, 2))]
graph = Graph(node_data=node_data, link_data=link_data, charge=-600, colors=['orange'] * 7)
Figure(marks=[graph], layout=fig_layout)
In [4]:
node_data = list('ABCDEFG')
#using link matrix to set links
link_matrix = np.zeros((7, 7))
xs = LinearScale()
ys = LinearScale()
x = [100, 200, 200, 300, 300, 300, 300]
y = [2, .5, 4, 8, 6, 4, 1]
graph = Graph(node_data=node_data, link_matrix=link_matrix, link_type='line',
colors=['orange'] * 7,
scales={'x': xs, 'y': ys, }, x=x, y=y,
directed=False)
Figure(marks=[graph], layout=fig_layout)
In [5]:
node_data = list('ABCDEFG')
#using link matrix to set links
link_matrix = np.zeros((7, 7))
xs = LinearScale(min=0, max=500)
ys = LinearScale(min=0, max=10)
cs = ColorScale(scheme='Reds')
x = [100, 200, 200, 300, 300, 300, 300]
y = [2, .5, 4, 8, 6, 4, 1]
graph = Graph(node_data=node_data, link_matrix=link_matrix, link_type='line',
color=np.random.rand(7),
scales={'x': xs, 'y': ys, 'color': cs}, x=x, y=y,
directed=False)
Figure(marks=[graph], layout=fig_layout)
In [6]:
node_data = list('ABCDEFG')
link_data = [{'source': s, 'target': t, 'value': np.random.rand()} for s, t in np.random.randint(0, 7, (20, 2))]
xs = LinearScale()
ys = LinearScale()
lcs = ColorScale(scheme='Greens')
x = [100, 200, 200, 300, 300, 300, 300]
y = [2, .5, 4, 8, 6, 4, 1]
graph = Graph(node_data=node_data, link_data=link_data, link_type='line',
colors=['orange'], directed=False,
scales={'x': xs, 'y': ys, 'link_color': lcs},
x=x, y=y, color=np.random.rand(7))
Figure(marks=[graph], layout=fig_layout)
In [7]:
node_data = [
{'label': 'A', 'shape': 'circle', 'shape_attrs': {'r': 20}, 'foo': 1},
{'label': 'Node B', 'shape': 'rect', 'shape_attrs': {'rx': 10, 'ry': 10, 'width': 40}, 'foo': 2},
{'label': 'C', 'shape': 'ellipse', 'foo': 4},
{'label': 'D', 'shape': 'rect', 'shape_attrs': {'width': 30, 'height': 30}, 'foo': 100},
]
link_data = [{'source': s, 'target': t, 'value': np.random.rand()} for s, t in np.random.randint(0, 4, (8, 2))]
graph = Graph(node_data=node_data, link_data=link_data, link_distance=150)
Figure(marks=[graph], layout=fig_layout)
In [8]:
tooltip = Tooltip(fields=['label', 'foo'], formats=['', '', ''])
graph.tooltip = tooltip
In [9]:
import bqplot.pyplot as plt
plt.clear()
line = plt.plot(np.cumsum(np.random.randn(20)))
# hover on nodes to see the plot
graph.tooltip = plt.current_figure()
In [10]:
graph.hovered_style = {'stroke': 'red'}
graph.unhovered_style = {'opacity': '0.4'}
graph.selected_style = {'opacity': '1', 'stroke': 'white', 'stroke-width': '2.5'}
In [11]:
# click on any node to select it (selected attribute gets populated with the index of the selected node)
graph.selected
Out[11]:
In [12]:
def print_event(self, target):
print(target)
graph.on_background_click(print_event)
graph.on_element_click(print_event)
In [ ]:
In [ ]:
In [ ]: