def communities(self, G): # Identify the largest maximal clique: largest_max_clique largest_max_clique = set( sorted(nx.find_cliques(G), key=lambda x: len(x))[-1]) # Create a subgraph from the largest_max_clique: G_lmc G_lmc = G.subgraph(largest_max_clique).copy() # Go out 1 degree of separation for node in list(G_lmc.nodes()): G_lmc.add_nodes_from(G.neighbors(node)) G_lmc.add_edges_from( zip([node] * len(list(G.neighbors(node))), G.neighbors(node))) # Record each node's degree centrality score for n in G_lmc.nodes(): G_lmc.node[n]['degree centrality'] = nx.degree_centrality(G_lmc)[n] # Create the ArcPlot object: a a = ArcPlot(G_lmc, node_order='degree centrality') # Draw the ArcPlot to the screen a.draw() plt.show()
def draw_graph(graph): # pos = graphviz_layout(graph, prog='twopi', args='') # plt.figure(figsize=(8, 8)) # nx.draw(graph, pos, node_size=20, alpha=0.5, node_color="blue", with_labels=False) # plt.axis('equal') # plt.show() # options = { # 'node_color': 'black', # 'node_size': 50, # 'line_color': 'grey', # 'linewidths': 0, # 'width': 0.1, # } # nx.draw(graph, **options) # plt.show() # Assume we have a professional network of physicians belonging to hospitals. # c = CircosPlot(graph, node_color='university', node_grouping='university') c = ArcPlot(graph, node_color="country", node_grouping="university", group_order="alphabetically") c.draw() plt.show() # only needed in scripts
def arc_plotting(self, G): # Iterate over all the nodes in G, including the metadata for n, d in G.nodes(data=True): # Calculate the degree of each node: G.node[n]['degree'] G.node[n]['degree'] = nx.degree(G, n) # Create the ArcPlot object: a a = ArcPlot(graph=G, node_order='degree') # Draw the ArcPlot to the screen a.draw() plt.show()
h.draw() plt.show() ################################## Task 2 (ArcPlot) # Import necessary modules from nxviz.plots import ArcPlot import matplotlib.pyplot as plt # Iterate over all the nodes in G, including the metadata for n, d in G.nodes(data=True): # Calculate the degree of each node: G.node[n]['degree'] G.node[n]['degree'] = nx.degree(G, n) # Create the ArcPlot object: a a = ArcPlot(graph=G, node_order='degree') # Draw the ArcPlot to the screen a.draw() plt.show() ############################### Task 3 (CircosPlot) # Import necessary modules from nxviz import CircosPlot import matplotlib.pyplot as plt # Iterate over all the nodes, including the metadata for n, d in G.nodes(data=True): # Calculate the degree of each node: G.node[n]['degree'] G.node[n]['degree'] = nx.degree(G, n)
""" Displays a NetworkX lollipop graph to screen using a ArcPlot. """ import matplotlib.pyplot as plt import networkx as nx import numpy.random as npr from nxviz.plots import ArcPlot G = nx.lollipop_graph(m=10, n=4) for n, d in G.nodes(data=True): G.node[n]['value'] = npr.normal() c = ArcPlot(G, node_color='value', node_order='value') c.draw() plt.show()
with open('../datasets/github_users_subsampled.p2', 'rb') as f: nodes, edges = pickle.load(f) G = nx.Graph() G.add_nodes_from(nodes) G.add_edges_from(edges) ''' Instructions * Make an ArcPlot of the GitHub collaboration network, with authors sorted by degree. To do this: * Iterate over all the nodes in G, including the metadata (by specifying data=True). * In each iteration of the loop, calculate the degree of each node n with nx.degree() and set its 'degree' attribute. nx.degree() accepts two arguments: A graph and a node. * Create the ArcPlot object a by specifying two parameters: the graph, which is G, and the node_order, which is 'degree', so that the nodes are sorted. * Draw the ArcPlot object to the screen. ''' # Import necessary modules from nxviz.plots import ArcPlot import matplotlib.pyplot as plt # Iterate over all the nodes in G, including the metadata for n, d in G.nodes(data=True): # Calculate the degree of each node: G.node[n]['degree'] G.node[n]['degree'] = nx.degree(G, n) # Create the ArcPlot object: a a = ArcPlot(graph=G, node_order='degree') # Draw the ArcPlot to the screen a.draw() plt.show()
/* Next up, let's use the ArcPlot to visualize the network. You're going to practice sorting the nodes in the graph as well. Note: this exercise may take about 4-7 seconds to execute if done correctly. */ # Import necessary modules from nxviz.plots import ArcPlot import matplotlib.pyplot as plt # Iterate over all the nodes in G, including the metadata for n, d in G.nodes(data=True): # Calculate the degree of each node: G.node[n]['degree'] G.node[n]['degree'] = nx.degree(G, n) # Create the ArcPlot object: a a = ArcPlot(G, node_order = 'degree') # Draw the ArcPlot to the screen a.draw() plt.show()
""" Displays a NetworkX barbell graph to screen using a ArcPlot. """ from nxviz.plots import ArcPlot from random import choice import networkx as nx import matplotlib.pyplot as plt G = nx.barbell_graph(m1=10, m2=3) for n, d in G.nodes(data=True): G.node[n]['class'] = choice(['one', 'two', 'three']) c = ArcPlot(G, node_color="class", node_order='class') c.draw() plt.show()
############## Diameter is the maximium eccentricity of the graph print(nx.radius(G)) ###### Periphery of a graph is(are) set(s) of node whose eccentricity == diamenter print(nx.periphery(G)) # Iterate over all the nodes in G, including the metadata for n, d in G.nodes(data=True): # Calculate the degree of each node: G.node[n]['degree'] G.node[n]['degree'] = nx.degree(G, n) G.node[n]["class"] = choice(["one", "two", "three"]) G.node[n]['distance'] = nx.eccentricity(G, n) # G.node[n]['center'] = nx.radius(G) a = ArcPlot(G, node_color="degree", node_order="distance", node_labels=True) # Draw the ArcPlot to the screen # print(G.node[3]) a.draw() plt.axis('off') plt.show() ###### Cemter nodes has eccentricity == radius print(nx.center(G)) # sensitive to outlier node # pos = nx.get_node_attributes(G, 'class') pos = nx.kamada_kawai_layout(G) nx.draw_networkx(G, pos) plt.axis('off') plt.show()
}), ('B', { 'n_visitors': '3' }), ('C', { 'n_visitors': '4' }), ] G.add_nodes_from(NODES_EBUNCH) EDGES_EBUNCH = [ ('A', 'B', 1), ('A', 'C', 2), ('B', 'C', 25), ('C', 'B', 10), ] G.add_weighted_edges_from(EDGES_EBUNCH, ) edges = G.edges() c = ArcPlot(G, node_labels=True, node_size='n_visitors', node_color='n_visitors', edge_width='weight') c.draw() plt.show()
""" Displays different node_size with ArcPlot """ import matplotlib.pyplot as plt import networkx as nx from nxviz.plots import ArcPlot G = nx.Graph() G.add_node("A", score=1.5) G.add_node("B", score=0.5) G.add_node("C", score=1) G.add_edge("A", "B", weight=8, type="a") G.add_edge("A", "C", weight=8, type="b") G.add_edge("B", "C", weight=8, type="a") c = ArcPlot(G, node_size="score", edge_width="weight", edge_color="type") c.draw() plt.show()
G = nx.Graph() G.add_edges_from(zip(df['doctor1'], df['doctor2'])) return G G = load_physicians_network() # Make a CircosPlot, but with the nodes colored by their connected component # subgraph ID. ccs = nx.connected_component_subgraphs(G) for i, g in enumerate(ccs): for n in g.nodes(): G.node[n]['group'] = i G.node[n]['connectivity'] = G.degree(n) m = CircosPlot(G, node_color='group', node_grouping='group', node_order='connectivity') m.draw() plt.show() # Make an ArcPlot. a = ArcPlot(G, node_color='group', node_grouping='group', node_order='connectivity') a.draw() plt.show()
""" Displays a NetworkX lollipop graph to screen using a ArcPlot. """ import matplotlib.pyplot as plt import networkx as nx import numpy.random as npr from nxviz.plots import ArcPlot G = nx.lollipop_graph(m=10, n=4) for n, d in G.nodes(data=True): G.node[n]["value"] = npr.normal() c = ArcPlot(G, node_color="value", node_order="value") c.draw() plt.show()
""" Displays different edge_colors with ArcPlot """ import matplotlib.pyplot as plt import networkx as nx from nxviz.plots import ArcPlot G = nx.Graph() G.add_edge("A", "B", weight=8, type="a") G.add_edge("A", "C", weight=8, type="b") G.add_edge("B", "C", weight=8, type="a") c = ArcPlot(G, edge_width="weight", edge_color="type") c.draw() plt.show()
""" Displays a NetworkX octahedral graph to screen using a ArcPlot. """ import matplotlib.pyplot as plt import networkx as nx from nxviz.plots import ArcPlot G = nx.octahedral_graph() c = ArcPlot(G) c.draw() plt.show()
G = nx.Graph() G.add_edges_from(zip(df["doctor1"], df["doctor2"])) return G G = load_physicians_network() # Make a CircosPlot, but with the nodes colored by their connected component # subgraph ID. ccs = nx.connected_component_subgraphs(G) for i, g in enumerate(ccs): for n in g.nodes(): G.node[n]["group"] = i G.node[n]["connectivity"] = G.degree(n) m = CircosPlot(G, node_color="group", node_grouping="group", node_order="connectivity") m.draw() plt.show() # Make an ArcPlot. a = ArcPlot(G, node_color="group", node_grouping="group", node_order="connectivity") a.draw() plt.show()