From 24a0b662950553c54787c320bb9cfb5c1d95f9ac Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Fri, 16 Oct 2020 14:23:42 -0700 Subject: 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. --- examples/drawing/plot_degree.py | 50 +++++++++++++++++++++++++++++++ examples/drawing/plot_degree_histogram.py | 35 ---------------------- examples/drawing/plot_degree_rank.py | 29 ------------------ 3 files changed, 50 insertions(+), 64 deletions(-) create mode 100644 examples/drawing/plot_degree.py delete mode 100644 examples/drawing/plot_degree_histogram.py delete mode 100644 examples/drawing/plot_degree_rank.py (limited to 'examples') 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() -- cgit v1.2.1