summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2020-10-16 14:03:08 -0700
committerGitHub <noreply@github.com>2020-10-16 14:03:08 -0700
commit864f1b8a610215e574867a61a5b83c1c44403bd6 (patch)
treed5cd71918c8f16434a0e3c751a312e853846c220 /examples
parent9262386b75762b25c935429864000464df41b7b5 (diff)
downloadnetworkx-864f1b8a610215e574867a61a5b83c1c44403bd6.tar.gz
Update giant component example (#4267)
* Update comment section about dependencies. Use try except. * Switch from plt to fig,ax approach for subplots * De-emphasize non-connected nodes in visualization * Make nodes more visible and refactor code * Seed example and remove layout try/except Co-authored-by: Jarrod Millman <jarrod.millman@gmail.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/drawing/plot_giant_component.py38
1 files changed, 21 insertions, 17 deletions
diff --git a/examples/drawing/plot_giant_component.py b/examples/drawing/plot_giant_component.py
index ce7101e4..e8fca7ef 100644
--- a/examples/drawing/plot_giant_component.py
+++ b/examples/drawing/plot_giant_component.py
@@ -13,43 +13,47 @@ import matplotlib.pyplot as plt
import networkx as nx
# This example needs Graphviz and either PyGraphviz or pydot.
-# from networkx.drawing.nx_pydot import graphviz_layout as layout
from networkx.drawing.nx_agraph import graphviz_layout as layout
-# If you don't have pygraphviz or pydot, you can do this
-# layout = nx.spring_layout
-
n = 150 # 150 nodes
# p value at which giant component (of size log(n) nodes) is expected
p_giant = 1.0 / (n - 1)
# p value at which graph is expected to become completely connected
-p_conn = math.log(n) / float(n)
+p_conn = math.log(n) / n
# the following range of p values should be close to the threshold
pvals = [0.003, 0.006, 0.008, 0.015]
-region = 220 # for pylab 2x2 subplot layout
-plt.subplots_adjust(left=0, right=1, bottom=0, top=0.95, wspace=0.01, hspace=0.01)
-for p in pvals:
- G = nx.binomial_graph(n, p)
- pos = layout(G)
- region += 1
- plt.subplot(region)
- plt.title(f"p = {p:.3f}")
- nx.draw(G, pos, with_labels=False, node_size=10)
+fig, axes = plt.subplots(2, 2)
+for p, ax, seed in zip(pvals, axes.ravel(), range(len(pvals))):
+ #### generate graph ####
+ G = nx.binomial_graph(n, p, seed=seed)
+ # identify connected/disconnected nodes
+ connected = [n for n, d in G.degree() if d > 0]
+ disconnected = list(set(G.nodes()) - set(connected))
# identify largest connected component
Gcc = sorted(nx.connected_components(G), key=len, reverse=True)
G0 = G.subgraph(Gcc[0])
- nx.draw_networkx_edges(G0, pos, edge_color="r", width=6.0)
- # show other connected components
+ #### draw graph ####
+ pos = layout(G)
+ ax.set_title(f"p = {p:.3f}")
+ # draw largest connected component
+ options = {"ax": ax, "edge_color": "tab:red"}
+ nx.draw_networkx_edges(G0, pos, width=6.0, **options)
+ # draw other connected components
for Gi in Gcc[1:]:
if len(Gi) > 1:
nx.draw_networkx_edges(
G.subgraph(Gi),
pos,
- edge_color="r",
alpha=0.3,
width=5.0,
+ **options,
)
+ # draw connected/disconnected nodes
+ options = {"ax": ax, "node_size": 30, "edgecolors": "white"}
+ nx.draw(G, pos, nodelist=connected, **options)
+ nx.draw(G, pos, nodelist=disconnected, alpha=0.25, **options)
+fig.tight_layout()
plt.show()