summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2020-10-16 14:23:42 -0700
committerGitHub <noreply@github.com>2020-10-16 14:23:42 -0700
commit24a0b662950553c54787c320bb9cfb5c1d95f9ac (patch)
tree8b7cde918e6f70c29dab47eaa43373cf3a13b974 /examples
parent824d4bc281c93f463ac1d937f32a029da80a6e22 (diff)
downloadnetworkx-24a0b662950553c54787c320bb9cfb5c1d95f9ac.tar.gz
Replace degree_histogram and degree_rank with a single example (#4265)
* Add rank plot and degree hist in one figure Condense the degree histogram and degree rank plot examples into a single example where the visualization inclues three things: 1. The subgraph of connected components 2. The degree rank plot 3. The degree histogram * Rename example and remove deg hist example. * Add summary of example to docstring and rename * Seeded graph gen and layout for reproduciblity.
Diffstat (limited to 'examples')
-rw-r--r--examples/drawing/plot_degree.py50
-rw-r--r--examples/drawing/plot_degree_histogram.py35
-rw-r--r--examples/drawing/plot_degree_rank.py29
3 files changed, 50 insertions, 64 deletions
diff --git a/examples/drawing/plot_degree.py b/examples/drawing/plot_degree.py
new file mode 100644
index 00000000..7670927f
--- /dev/null
+++ b/examples/drawing/plot_degree.py
@@ -0,0 +1,50 @@
+"""
+===============
+Degree Analysis
+===============
+
+This example shows several ways to visualize the distribution of the degree of
+nodes with two common techniques: a *degree-rank plot* and a
+*degree histogram*.
+
+In this example, a random Graph is generated with 100 nodes. The degree of
+each node is determined, and a figure is generated showing three things:
+ 1. The subgraph of connected components
+ 2. The degree-rank plot for the Graph, and
+ 3. The degree histogram
+"""
+import networkx as nx
+import numpy as np
+import matplotlib.pyplot as plt
+
+G = nx.gnp_random_graph(100, 0.02, seed=10374196)
+
+degree_sequence = sorted([d for n, d in G.degree()], reverse=True)
+dmax = max(degree_sequence)
+
+fig = plt.figure("Degree of a random graph", figsize=(8, 8))
+# Create a gridspec for adding subplots of different sizes
+axgrid = fig.add_gridspec(5, 4)
+
+ax0 = fig.add_subplot(axgrid[0:3, :])
+Gcc = G.subgraph(sorted(nx.connected_components(G), key=len, reverse=True)[0])
+pos = nx.spring_layout(Gcc, seed=10396953)
+nx.draw_networkx_nodes(Gcc, pos, ax=ax0, node_size=20)
+nx.draw_networkx_edges(Gcc, pos, ax=ax0, alpha=0.4)
+ax0.set_title("Connected components of G")
+ax0.set_axis_off()
+
+ax1 = fig.add_subplot(axgrid[3:, :2])
+ax1.plot(degree_sequence, "b-", marker="o")
+ax1.set_title("Degree Rank Plot")
+ax1.set_ylabel("Degree")
+ax1.set_xlabel("Rank")
+
+ax2 = fig.add_subplot(axgrid[3:, 2:])
+ax2.bar(*np.unique(degree_sequence, return_counts=True))
+ax2.set_title("Degree histogram")
+ax2.set_xlabel("Degree")
+ax2.set_ylabel("# of Nodes")
+
+fig.tight_layout()
+plt.show()
diff --git a/examples/drawing/plot_degree_histogram.py b/examples/drawing/plot_degree_histogram.py
deleted file mode 100644
index 86d6b7c3..00000000
--- a/examples/drawing/plot_degree_histogram.py
+++ /dev/null
@@ -1,35 +0,0 @@
-"""
-================
-Degree histogram
-================
-
-Draw degree histogram with matplotlib.
-Random graph shown as inset
-"""
-import collections
-import matplotlib.pyplot as plt
-import networkx as nx
-
-G = nx.gnp_random_graph(100, 0.02)
-
-degree_sequence = sorted([d for n, d in G.degree()], reverse=True) # degree sequence
-degreeCount = collections.Counter(degree_sequence)
-deg, cnt = zip(*degreeCount.items())
-
-fig, ax = plt.subplots()
-plt.bar(deg, cnt, width=0.80, color="b")
-
-plt.title("Degree Histogram")
-plt.ylabel("Count")
-plt.xlabel("Degree")
-ax.set_xticks([d + 0.4 for d in deg])
-ax.set_xticklabels(deg)
-
-# draw graph in inset
-plt.axes([0.4, 0.4, 0.5, 0.5])
-Gcc = G.subgraph(sorted(nx.connected_components(G), key=len, reverse=True)[0])
-pos = nx.spring_layout(G)
-plt.axis("off")
-nx.draw_networkx_nodes(G, pos, node_size=20)
-nx.draw_networkx_edges(G, pos, alpha=0.4)
-plt.show()
diff --git a/examples/drawing/plot_degree_rank.py b/examples/drawing/plot_degree_rank.py
deleted file mode 100644
index 61039cc5..00000000
--- a/examples/drawing/plot_degree_rank.py
+++ /dev/null
@@ -1,29 +0,0 @@
-"""
-===========
-Degree Rank
-===========
-
-Random graph from given degree sequence.
-Draw degree rank plot and graph with matplotlib.
-"""
-import networkx as nx
-import matplotlib.pyplot as plt
-
-G = nx.gnp_random_graph(100, 0.02)
-
-degree_sequence = sorted([d for n, d in G.degree()], reverse=True)
-dmax = max(degree_sequence)
-
-plt.loglog(degree_sequence, "b-", marker="o")
-plt.title("Degree rank plot")
-plt.ylabel("degree")
-plt.xlabel("rank")
-
-# draw graph in inset
-plt.axes([0.45, 0.45, 0.45, 0.45])
-Gcc = G.subgraph(sorted(nx.connected_components(G), key=len, reverse=True)[0])
-pos = nx.spring_layout(Gcc)
-plt.axis("off")
-nx.draw_networkx_nodes(Gcc, pos, node_size=20)
-nx.draw_networkx_edges(Gcc, pos, alpha=0.4)
-plt.show()