summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJarrod Millman <jarrod.millman@gmail.com>2020-12-07 18:00:22 -0800
committerGitHub <noreply@github.com>2020-12-07 21:00:22 -0500
commitb8ca636525e12f13a491c3d738c965fe1ce7851c (patch)
tree6bfd67cd5da8d96e8b35f02fdda2bbcf926e2c0e /examples
parent6108e42fa70fec2d1af0f177bf5518d6b60981d6 (diff)
downloadnetworkx-b8ca636525e12f13a491c3d738c965fe1ce7851c.tar.gz
Add igraph example (#4404)
* Add igraph example * Update comment to mention upcoming igraph release
Diffstat (limited to 'examples')
-rw-r--r--examples/external/plot_igraph.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/external/plot_igraph.py b/examples/external/plot_igraph.py
new file mode 100644
index 00000000..1e85c7b8
--- /dev/null
+++ b/examples/external/plot_igraph.py
@@ -0,0 +1,44 @@
+"""
+======
+igraph
+======
+
+igraph (https://igraph.org/) is a popular network analysis package that
+provides (among many other things) functions to convert to/from NetworkX.
+"""
+
+import matplotlib.pyplot as plt
+import networkx as nx
+import igraph as ig
+
+# %%
+# NetworkX to igraph
+# ------------------
+
+G = nx.dense_gnm_random_graph(1000, 2000)
+
+# largest connected component
+components = nx.connected_components(G)
+largest_component = max(components, key=len)
+H = G.subgraph(largest_component)
+
+# networkx draw
+nx.draw(H)
+plt.show()
+
+# convert to igraph
+g = ig.Graph.from_networkx(G)
+
+## TODO: uncomment this once python-igraph 0.9 is released
+# igraph draw
+# layout = g.layout()
+# ig.plot(g, layout=layout)
+
+# %%
+# igraph to NetworkX
+# ------------------
+
+g = ig.Graph.GRG(100, 0.2)
+G = g.to_networkx()
+nx.draw(G)
+plt.show()