{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "var chart_idx='dqyohqufvjgpkzts', graphx={\"multigraph\": false, \"nodes\": [{\"id\": 0}, {\"id\": 1}, {\"id\": 2}, {\"id\": 3}, {\"id\": 4}, {\"id\": 5}, {\"id\": 6}, {\"id\": 7}, {\"id\": 8}, {\"id\": 9}], \"graph\": {\"name\": \"Petersen Graph\"}, \"links\": [{\"target\": 1, \"source\": 0}, {\"target\": 4, \"source\": 0}, {\"target\": 5, \"source\": 0}, {\"target\": 2, \"source\": 1}, {\"target\": 6, \"source\": 1}, {\"target\": 3, \"source\": 2}, {\"target\": 7, \"source\": 2}, {\"target\": 8, \"source\": 3}, {\"target\": 4, \"source\": 3}, {\"target\": 9, \"source\": 4}, {\"target\": 8, \"source\": 5}, {\"target\": 7, \"source\": 5}, {\"target\": 8, \"source\": 6}, {\"target\": 9, \"source\": 6}, {\"target\": 9, \"source\": 7}], \"directed\": false}, widthx=960, heightx=600;\n", "var process_nx = function(d3, chart_id, graph, width, height) {\n", " var color = d3.scale.category20();\n", "\n", " var force = d3.layout.force()\n", " .charge(-200)\n", " .linkDistance(40)\n", " .size([width, height]);\n", "\n", " var svg = d3.select('#' + chart_id).append(\"svg\")\n", " .attr(\"width\", width)\n", " .attr(\"height\", height);\n", "\n", " force\n", " .nodes(graph.nodes)\n", " .links(graph.links)\n", " .start();\n", "\n", " var link = svg.selectAll(\".link\")\n", " .data(graph.links)\n", " .enter().append(\"line\")\n", " .attr(\"class\", \"link\")\n", " .attr(\"stroke\", \"#999\")\n", " .attr(\"stroke-width\", 1.5);\n", "\n", " var node = svg.selectAll(\".node\")\n", " .data(graph.nodes)\n", " .enter().append(\"g\")\n", " .attr(\"class\", \"node\")\n", " .call(force.drag);\n", "\n", " node.append(\"circle\")\n", " .attr(\"r\", 6)\n", " .style(\"fill\", function(d) {\n", " return color(d.color);\n", " });\n", "\n", " node.append(\"text\")\n", " .attr(\"dx\", 12)\n", " .attr(\"dy\", \".35em\")\n", " .text(function(d) {\n", " return d.id;\n", " });\n", "\n", " force.on(\"tick\", function() {\n", " link.attr(\"x1\", function(d) { return d.source.x; })\n", " .attr(\"y1\", function(d) { return d.source.y; })\n", " .attr(\"x2\", function(d) { return d.target.x; })\n", " .attr(\"y2\", function(d) { return d.target.y; });\n", "\n", " node.attr(\"transform\", function(d) {\n", " return \"translate(\" + d.x + \",\" + d.y + \")\";\n", " });\n", " });\n", "}\n", "\n", " require.config({\n", " paths: {\n", " d3: '//cdnjs.cloudflare.com/ajax/libs/d3/3.4.8/d3.min'\n", " }\n", " });\n", "\n", " element.append(\"<div id='\" + chart_idx + \"'></div>\");\n", "\n", " require(['d3'], function(d3) {\n", " return process_nx(d3, chart_idx, graphx, widthx, heightx)\n", " })\n", " " ], "text/plain": [ "<IPython.core.display.Javascript object>" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import networkx as nx\n", "import nx2d3\n", "G = nx.petersen_graph()\n", "nx2d3.embed_networkx(G)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }