from itertools import chain
from bqplot import OrdinalColorScale, Figure, Graph
from ipywidgets import Layout
# mobile patent suits - http://bl.ocks.org/mbostock/1153292
suits = [
{'source': 'Microsoft', 'target': 'Amazon', 'type': 'licensing'},
{'source': 'Microsoft', 'target': 'HTC', 'type': 'licensing'},
{'source': 'Samsung', 'target': 'Apple', 'type': 'suit'},
{'source': 'Motorola', 'target': 'Apple', 'type': 'suit'},
{'source': 'Nokia', 'target': 'Apple', 'type': 'resolved'},
{'source': 'HTC', 'target': 'Apple', 'type': 'suit'},
{'source': 'Kodak', 'target': 'Apple', 'type': 'suit'},
{'source': 'Microsoft', 'target': 'Barnes & Noble', 'type': 'suit'},
{'source': 'Microsoft', 'target': 'Foxconn', 'type': 'suit'},
{'source': 'Oracle', 'target': 'Google', 'type': 'suit'},
{'source': 'Apple', 'target': 'HTC', 'type': 'suit'},
{'source': 'Microsoft', 'target': 'Inventec', 'type': 'suit'},
{'source': 'Samsung', 'target': 'Kodak', 'type': 'resolved'},
{'source': 'LG', 'target': 'Kodak', 'type': 'resolved'},
{'source': 'RIM', 'target': 'Kodak', 'type': 'suit'},
{'source': 'Sony', 'target': 'LG', 'type': 'suit'},
{'source': 'Kodak', 'target': 'LG', 'type': 'resolved'},
{'source': 'Apple', 'target': 'Nokia', 'type': 'resolved'},
{'source': 'Qualcomm', 'target': 'Nokia', 'type': 'resolved'},
{'source': 'Apple', 'target': 'Motorola', 'type': 'suit'},
{'source': 'Microsoft', 'target': 'Motorola', 'type': 'suit'},
{'source': 'Motorola', 'target': 'Microsoft', 'type': 'suit'},
{'source': 'Huawei', 'target': 'ZTE', 'type': 'suit'},
{'source': 'Ericsson', 'target': 'ZTE', 'type': 'suit'},
{'source': 'Kodak', 'target': 'Samsung', 'type': 'resolved'},
{'source': 'Apple', 'target': 'Samsung', 'type': 'suit'},
{'source': 'Kodak', 'target': 'RIM', 'type': 'suit'},
{'source': 'Nokia', 'target': 'Qualcomm', 'type': 'suit'},
]
# transform data into nodes and links
nodes = list(set(chain(*((suit['source'], suit['target']) for suit in suits))))
# set custom node attrs
node_data = [{'label': node, 'shape_attrs': {'r': 6}, 'label_display': 'outside'} for node in nodes]
# for links, source and target should be indices into the nodes list
nodes_index_map = {node: i for i, node in enumerate(nodes)}
link_data = [{'source': nodes_index_map[s['source']],
'target': nodes_index_map[s['target']],
'value': s['type']} for s in suits]
# encode suit type with link color
link_color_scale = OrdinalColorScale(domain=['licensing', 'suit', 'resolved'],
colors=['limegreen', 'dodgerblue', 'orangered'])
graph = Graph(node_data=node_data, link_data=link_data, link_type='arc',
scales={'link_color': link_color_scale}, colors=['gray'],
directed=True, link_distance=100, charge=-600)
Figure(marks=[graph], layout=Layout(height='900px', width='1000px'), title='Mobile Patent Suits')
Failed to display Jupyter Widget of type Figure
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.