| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "code", |
| 5 |
"execution_count": null, |
| 6 |
"metadata": {}, |
| 7 |
"outputs": [], |
| 8 |
"source": [ |
| 9 |
"import numpy as np\n", |
| 10 |
"from bqplot import Graph, LinearScale, ColorScale, Figure, Tooltip\n", |
| 11 |
"from ipywidgets import Layout" |
| 12 |
] |
| 13 |
}, |
| 14 |
{ |
| 15 |
"cell_type": "markdown", |
| 16 |
"metadata": {}, |
| 17 |
"source": [ |
| 18 |
"Nodes and Links should be supplied to the Graph mark. \n", |
| 19 |
"<p>Node attributes\n", |
| 20 |
"\n", |
| 21 |
"| Attribute| Type | Description | Default |\n", |
| 22 |
"|:----------:|:-------------:|:------:|\n", |
| 23 |
"| label | str | node label | mandatory attribute |\n", |
| 24 |
"| label_display | {center, outside, none} | label display options | center |\n", |
| 25 |
"| shape | {circle, ellipse, rect} | node shape | circle |\n", |
| 26 |
"| shape_attrs | dict | [node SVG attributes](https://www.w3.org/TR/SVG/shapes.html) | {'r': 15} |\n", |
| 27 |
"<p> Link Attributes\n", |
| 28 |
"\n", |
| 29 |
"| Attribute| Type | Description | Default |\n", |
| 30 |
"|:----------:|:-------------:|:------:|\n", |
| 31 |
"| source | int | source node index | mandatory attribute |\n", |
| 32 |
"| target | int | target node index | mandatory attribute |\n", |
| 33 |
"| value | float | value of the link. Use np.nan if you do not want a link| - |\n", |
| 34 |
"\n", |
| 35 |
"<p> 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\n", |
| 36 |
"\n", |
| 37 |
"<p> Link Data can be passed in through 3 data attributes - \n", |
| 38 |
"1. link_data - list of dicts (e.g. [{'source': 0, 'target': 1, 'value': 10}, {'source': 2, 'target': 1, 'value': 20},...]\n", |
| 39 |
"2. link_matrix - 2-d numpy array of shape (n, n) where n is the number of nodes\n", |
| 40 |
"3. link_color - 2-d numpy array of shape (n, n) where n is the number of nodes. This attribute can be used to encode the link_color by passing in a link_color scale\n" |
| 41 |
] |
| 42 |
}, |
| 43 |
{ |
| 44 |
"cell_type": "code", |
| 45 |
"execution_count": null, |
| 46 |
"metadata": {}, |
| 47 |
"outputs": [], |
| 48 |
"source": [ |
| 49 |
"fig_layout = Layout(width='960px', height='500px')" |
| 50 |
] |
| 51 |
}, |
| 52 |
{ |
| 53 |
"cell_type": "markdown", |
| 54 |
"metadata": {}, |
| 55 |
"source": [ |
| 56 |
"### Directed Graph\n", |
| 57 |
"If we dont specify the x and y location for nodes then the force layout will set the location of nodes" |
| 58 |
] |
| 59 |
}, |
| 60 |
{ |
| 61 |
"cell_type": "code", |
| 62 |
"execution_count": null, |
| 63 |
"metadata": {}, |
| 64 |
"outputs": [], |
| 65 |
"source": [ |
| 66 |
"node_data = [\n", |
| 67 |
" dict(label='A', shape='rect'),\n", |
| 68 |
" dict(label='B', shape='ellipse'),\n", |
| 69 |
" dict(label='C', shape='ellipse'),\n", |
| 70 |
" dict(label='D', shape='rect'),\n", |
| 71 |
" dict(label='E', shape='ellipse'),\n", |
| 72 |
" dict(label='F', shape='circle'),\n", |
| 73 |
" dict(label='G', shape='ellipse'),\n", |
| 74 |
"]\n", |
| 75 |
"link_data = [{'source': s, 'target': t} for s, t in np.random.randint(0, 7, (10, 2)) if s != t]\n", |
| 76 |
"graph = Graph(node_data=node_data, link_data=link_data, charge=-600, colors=['orange'] * 7)\n", |
| 77 |
"Figure(marks=[graph], layout=fig_layout)" |
| 78 |
] |
| 79 |
}, |
| 80 |
{ |
| 81 |
"cell_type": "code", |
| 82 |
"execution_count": null, |
| 83 |
"metadata": {}, |
| 84 |
"outputs": [], |
| 85 |
"source": [ |
| 86 |
"graph.link_type = 'line'" |
| 87 |
] |
| 88 |
}, |
| 89 |
{ |
| 90 |
"cell_type": "markdown", |
| 91 |
"metadata": {}, |
| 92 |
"source": [ |
| 93 |
"### Explicitly setting x and y location for nodes using x and y scales. \n", |
| 94 |
"Force layout animation is disabled in this case" |
| 95 |
] |
| 96 |
}, |
| 97 |
{ |
| 98 |
"cell_type": "code", |
| 99 |
"execution_count": null, |
| 100 |
"metadata": {}, |
| 101 |
"outputs": [], |
| 102 |
"source": [ |
| 103 |
"node_data = list('ABCDEFG')\n", |
| 104 |
"\n", |
| 105 |
"#using link matrix to set links\n", |
| 106 |
"link_matrix = np.zeros((7, 7))\n", |
| 107 |
"xs = LinearScale()\n", |
| 108 |
"ys = LinearScale()\n", |
| 109 |
"x = [100, 200, 200, 300, 300, 300, 300]\n", |
| 110 |
"y = [2, .5, 4, 8, 6, 4, 1]\n", |
| 111 |
"graph = Graph(node_data=node_data, link_matrix=link_matrix, link_type='line', \n", |
| 112 |
" colors=['orange'] * 7,\n", |
| 113 |
" scales={'x': xs, 'y': ys, }, x=x, y=y, \n", |
| 114 |
" directed=False)\n", |
| 115 |
"Figure(marks=[graph], layout=fig_layout)" |
| 116 |
] |
| 117 |
}, |
| 118 |
{ |
| 119 |
"cell_type": "markdown", |
| 120 |
"metadata": {}, |
| 121 |
"source": [ |
| 122 |
"### Color code nodes using 'color' data attribute" |
| 123 |
] |
| 124 |
}, |
| 125 |
{ |
| 126 |
"cell_type": "code", |
| 127 |
"execution_count": null, |
| 128 |
"metadata": {}, |
| 129 |
"outputs": [], |
| 130 |
"source": [ |
| 131 |
"node_data = list('ABCDEFG')\n", |
| 132 |
"\n", |
| 133 |
"#using link matrix to set links\n", |
| 134 |
"link_matrix = np.zeros((7, 7))\n", |
| 135 |
"xs = LinearScale(min=0, max=500)\n", |
| 136 |
"ys = LinearScale(min=0, max=10)\n", |
| 137 |
"cs = ColorScale(scheme='Reds')\n", |
| 138 |
"x = [100, 200, 200, 300, 300, 300, 300]\n", |
| 139 |
"y = [2, .5, 4, 8, 6, 4, 1]\n", |
| 140 |
"graph = Graph(node_data=node_data, link_matrix=link_matrix, link_type='line', \n", |
| 141 |
" color=np.random.rand(7),\n", |
| 142 |
" scales={'x': xs, 'y': ys, 'color': cs}, x=x, y=y, \n", |
| 143 |
" directed=False)\n", |
| 144 |
"Figure(marks=[graph], layout=fig_layout)" |
| 145 |
] |
| 146 |
}, |
| 147 |
{ |
| 148 |
"cell_type": "markdown", |
| 149 |
"metadata": {}, |
| 150 |
"source": [ |
| 151 |
"### Color code links using link_color scale" |
| 152 |
] |
| 153 |
}, |
| 154 |
{ |
| 155 |
"cell_type": "code", |
| 156 |
"execution_count": null, |
| 157 |
"metadata": {}, |
| 158 |
"outputs": [], |
| 159 |
"source": [ |
| 160 |
"node_data = list('ABCDEFG')\n", |
| 161 |
"\n", |
| 162 |
"link_data = [{'source': s, 'target': t, 'value': np.random.rand()} for s, t in np.random.randint(0, 7, (20, 2))]\n", |
| 163 |
"\n", |
| 164 |
"xs = LinearScale()\n", |
| 165 |
"ys = LinearScale()\n", |
| 166 |
"lcs = ColorScale(scheme='Greens')\n", |
| 167 |
"x = [100, 200, 200, 300, 300, 300, 300]\n", |
| 168 |
"y = [2, .5, 4, 8, 6, 4, 1]\n", |
| 169 |
"graph = Graph(node_data=node_data, link_data=link_data, link_type='line',\n", |
| 170 |
" colors=['orange'], directed=False, \n", |
| 171 |
" scales={'x': xs, 'y': ys, 'link_color': lcs}, \n", |
| 172 |
" x=x, y=y, color=np.random.rand(7))\n", |
| 173 |
"Figure(marks=[graph], layout=fig_layout)" |
| 174 |
] |
| 175 |
}, |
| 176 |
{ |
| 177 |
"cell_type": "markdown", |
| 178 |
"metadata": {}, |
| 179 |
"source": [ |
| 180 |
"### Custom node attributes" |
| 181 |
] |
| 182 |
}, |
| 183 |
{ |
| 184 |
"cell_type": "code", |
| 185 |
"execution_count": null, |
| 186 |
"metadata": { |
| 187 |
"scrolled": false |
| 188 |
}, |
| 189 |
"outputs": [], |
| 190 |
"source": [ |
| 191 |
"node_data = [\n", |
| 192 |
" {'label': 'A', 'shape': 'circle', 'shape_attrs': {'r': 20}, 'foo': 1},\n", |
| 193 |
" {'label': 'Node B', 'shape': 'rect', 'shape_attrs': {'rx': 10, 'ry': 10, 'width': 40}, 'foo': 2},\n", |
| 194 |
" {'label': 'C', 'shape': 'ellipse', 'foo': 4},\n", |
| 195 |
" {'label': 'D', 'shape': 'rect', 'shape_attrs': {'width': 30, 'height': 30}, 'foo': 100},\n", |
| 196 |
"]\n", |
| 197 |
"\n", |
| 198 |
"link_data = [{'source': s, 'target': t, 'value': np.random.rand()} for s, t in np.random.randint(0, 4, (8, 2))]\n", |
| 199 |
"\n", |
| 200 |
"graph = Graph(node_data=node_data, link_data=link_data, link_distance=150)\n", |
| 201 |
"Figure(marks=[graph], layout=fig_layout)" |
| 202 |
] |
| 203 |
}, |
| 204 |
{ |
| 205 |
"cell_type": "markdown", |
| 206 |
"metadata": {}, |
| 207 |
"source": [ |
| 208 |
"### Add tooltips on nodes" |
| 209 |
] |
| 210 |
}, |
| 211 |
{ |
| 212 |
"cell_type": "code", |
| 213 |
"execution_count": null, |
| 214 |
"metadata": {}, |
| 215 |
"outputs": [], |
| 216 |
"source": [ |
| 217 |
"tooltip = Tooltip(fields=['label', 'foo'], formats=['', '', ''])\n", |
| 218 |
"graph.tooltip = tooltip" |
| 219 |
] |
| 220 |
}, |
| 221 |
{ |
| 222 |
"cell_type": "markdown", |
| 223 |
"metadata": {}, |
| 224 |
"source": [ |
| 225 |
"### Add a line chart as a tooltip" |
| 226 |
] |
| 227 |
}, |
| 228 |
{ |
| 229 |
"cell_type": "code", |
| 230 |
"execution_count": null, |
| 231 |
"metadata": {}, |
| 232 |
"outputs": [], |
| 233 |
"source": [ |
| 234 |
"import bqplot.pyplot as plt\n", |
| 235 |
"plt.clear()\n", |
| 236 |
"line = plt.plot(np.cumsum(np.random.randn(20)))\n", |
| 237 |
"# hover on nodes to see the plot\n", |
| 238 |
"graph.tooltip = plt.current_figure()" |
| 239 |
] |
| 240 |
}, |
| 241 |
{ |
| 242 |
"cell_type": "markdown", |
| 243 |
"metadata": {}, |
| 244 |
"source": [ |
| 245 |
"### Custom styling of hovered and selected nodes" |
| 246 |
] |
| 247 |
}, |
| 248 |
{ |
| 249 |
"cell_type": "code", |
| 250 |
"execution_count": null, |
| 251 |
"metadata": {}, |
| 252 |
"outputs": [], |
| 253 |
"source": [ |
| 254 |
"graph.hovered_style = {'stroke': 'red'}\n", |
| 255 |
"graph.unhovered_style = {'opacity': '0.4'}\n", |
| 256 |
"\n", |
| 257 |
"graph.selected_style = {'opacity': '1', 'stroke': 'white', 'stroke-width': '2.5'}" |
| 258 |
] |
| 259 |
}, |
| 260 |
{ |
| 261 |
"cell_type": "code", |
| 262 |
"execution_count": null, |
| 263 |
"metadata": {}, |
| 264 |
"outputs": [], |
| 265 |
"source": [ |
| 266 |
"# click on any node to select it (selected attribute gets populated with the index of the selected node)\n", |
| 267 |
"graph.selected" |
| 268 |
] |
| 269 |
}, |
| 270 |
{ |
| 271 |
"cell_type": "markdown", |
| 272 |
"metadata": {}, |
| 273 |
"source": [ |
| 274 |
"### Add element click and background click handlers" |
| 275 |
] |
| 276 |
}, |
| 277 |
{ |
| 278 |
"cell_type": "code", |
| 279 |
"execution_count": null, |
| 280 |
"metadata": {}, |
| 281 |
"outputs": [], |
| 282 |
"source": [ |
| 283 |
"def print_event(self, target):\n", |
| 284 |
" print(target)\n", |
| 285 |
"\n", |
| 286 |
"graph.on_background_click(print_event)\n", |
| 287 |
"graph.on_element_click(print_event)" |
| 288 |
] |
| 289 |
} |
| 290 |
], |
| 291 |
"metadata": { |
| 292 |
"input_collapsed": false, |
| 293 |
"kernelspec": { |
| 294 |
"display_name": "Python 3", |
| 295 |
"language": "python", |
| 296 |
"name": "python3" |
| 297 |
}, |
| 298 |
"language_info": { |
| 299 |
"codemirror_mode": { |
| 300 |
"name": "ipython", |
| 301 |
"version": 3 |
| 302 |
}, |
| 303 |
"file_extension": ".py", |
| 304 |
"mimetype": "text/x-python", |
| 305 |
"name": "python", |
| 306 |
"nbconvert_exporter": "python", |
| 307 |
"pygments_lexer": "ipython3", |
| 308 |
"version": "3.9.2" |
| 309 |
} |
| 310 |
}, |
| 311 |
"nbformat": 4, |
| 312 |
"nbformat_minor": 2 |
| 313 |
} |