summaryrefslogtreecommitdiff
path: root/examples/basic/plot_properties.py
diff options
context:
space:
mode:
authorJarrod Millman <jarrod.millman@gmail.com>2017-07-21 08:50:40 -0700
committerDan Schult <dschult@colgate.edu>2017-07-21 11:50:40 -0400
commit1e6df4c105fa7a1e442da72ddb009bb253dc7d4e (patch)
treed728526ccca673a762f90f174223cb558c86e02d /examples/basic/plot_properties.py
parent7be0d8f33b46d37264ae181cb2a9fadb310fe687 (diff)
downloadnetworkx-1e6df4c105fa7a1e442da72ddb009bb253dc7d4e.tar.gz
Refactor examples (#2527)
* Fix unix_email and plot more examples * Fix doctest * Fix more examples * Fix AntiGraph.degree generator error in examples * Style/consistency changes and plot more examples
Diffstat (limited to 'examples/basic/plot_properties.py')
-rw-r--r--examples/basic/plot_properties.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/basic/plot_properties.py b/examples/basic/plot_properties.py
new file mode 100644
index 00000000..30e055df
--- /dev/null
+++ b/examples/basic/plot_properties.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+"""
+==========
+Properties
+==========
+
+Compute some network properties for the lollipop graph.
+"""
+# Copyright (C) 2004-2017 by
+# Aric Hagberg <hagberg@lanl.gov>
+# Dan Schult <dschult@colgate.edu>
+# Pieter Swart <swart@lanl.gov>
+# All rights reserved.
+# BSD license.
+
+import matplotlib.pyplot as plt
+from networkx import nx
+
+G = nx.lollipop_graph(4, 6)
+
+pathlengths = []
+
+print("source vertex {target:length, }")
+for v in G.nodes():
+ spl = dict(nx.single_source_shortest_path_length(G, v))
+ print('{} {} '.format(v, spl))
+ for p in spl:
+ pathlengths.append(spl[p])
+
+print('')
+print("average shortest path length %s" % (sum(pathlengths) / len(pathlengths)))
+
+# histogram of path lengths
+dist = {}
+for p in pathlengths:
+ if p in dist:
+ dist[p] += 1
+ else:
+ dist[p] = 1
+
+print('')
+print("length #paths")
+verts = dist.keys()
+for d in sorted(verts):
+ print('%s %d' % (d, dist[d]))
+
+print("radius: %d" % nx.radius(G))
+print("diameter: %d" % nx.diameter(G))
+print("eccentricity: %s" % nx.eccentricity(G))
+print("center: %s" % nx.center(G))
+print("periphery: %s" % nx.periphery(G))
+print("density: %s" % nx.density(G))
+
+nx.draw(G, with_labels=True)
+plt.show()