summaryrefslogtreecommitdiff
path: root/_downloads/d8d3d37031dc95f0d814575af4ac51f7/plot_igraph.ipynb
diff options
context:
space:
mode:
Diffstat (limited to '_downloads/d8d3d37031dc95f0d814575af4ac51f7/plot_igraph.ipynb')
-rw-r--r--_downloads/d8d3d37031dc95f0d814575af4ac51f7/plot_igraph.ipynb90
1 files changed, 90 insertions, 0 deletions
diff --git a/_downloads/d8d3d37031dc95f0d814575af4ac51f7/plot_igraph.ipynb b/_downloads/d8d3d37031dc95f0d814575af4ac51f7/plot_igraph.ipynb
new file mode 100644
index 00000000..07200afe
--- /dev/null
+++ b/_downloads/d8d3d37031dc95f0d814575af4ac51f7/plot_igraph.ipynb
@@ -0,0 +1,90 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "%matplotlib inline"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "\n# igraph\n\nigraph (https://igraph.org/) is a popular network analysis package that\nprovides (among many other things) functions to convert to/from NetworkX.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import matplotlib.pyplot as plt\nimport networkx as nx\nimport igraph as ig"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## NetworkX to igraph\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "G = nx.dense_gnm_random_graph(30, 40, seed=42)\n\n# largest connected component\ncomponents = nx.connected_components(G)\nlargest_component = max(components, key=len)\nH = G.subgraph(largest_component)\n\n# convert to igraph\nh = ig.Graph.from_networkx(H)\n\n\n# Plot the same network with NetworkX and igraph\nfig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))\n\n# NetworkX draw\nax0.set_title(\"Plot with NetworkX draw\")\nnx.draw_kamada_kawai(H, node_size=50, ax=ax0)\n\n# igraph draw\nax1.set_title(\"Plot with igraph plot\")\nlayout = h.layout_kamada_kawai()\nig.plot(h, layout=layout, target=ax1)\nplt.axis(\"off\")\nplt.show()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## igraph to NetworkX\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "g = ig.Graph.GRG(30, 0.2)\nG = g.to_networkx()\nnx.draw(G, node_size=50)\nplt.show()"
+ ]
+ }
+ ],
+ "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.9.15"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+} \ No newline at end of file