Record each node's degree centrality score in its node metadata. Do this by assigning nx.degree_centrality(G_lmc)[n] to G_lmc.node[n]['degree centrality'] in the second for loop. Visualize this network with an ArcPlot sorting the nodes by degree centrality (you can do this using the keyword argument node_order='degree centrality'). ''' SOLUTION # Import necessary modules from nxviz import ArcPlot import matplotlib.pyplot as plt # 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(graph=G_lmc, node_order='degree centrality') # Draw the ArcPlot to the screen a.draw() plt.show()
Add nodes to G_lmc from the neighbors of G using the .add_nodes_from() method and .neighbors() methods. Using the .add_edges_from(), method, add edges to G_lmc between the current node and all its neighbors. To do this, you'll have create a list of tuples using the zip() function consisting of the current node and each of its neighbors. The first argument to zip() should be [node]*len(G.neighbors(node)), and the second argument should be the neighbors of node. Record each node's degree centrality score in its node metadata. Do this by assigning nx.degree_centrality(G_lmc)[n] to G_lmc.node[n]['degree centrality'] in the second for loop. Visualize this network with an ArcPlot sorting the nodes by degree centrality (you can do this using the keyword argument node_order='degree centrality'). ''' # Import necessary modules from nxviz import ArcPlot import matplotlib.pyplot as plt # 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) # Go out 1 degree of separation for node in G_lmc.nodes(): G_lmc.add_nodes_from(G.neighbors(node)) G_lmc.add_edges_from(zip([node]*len(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()
c = CircosPlot(T) # Draw c to the screen c.draw() # Display the plot plt.show() #7 # Import necessary modules import matplotlib.pyplot as plt from nxviz import ArcPlot # Create the un-customized ArcPlot object: a a = ArcPlot(T) # Draw a to the screen a.draw() # Display the plot plt.show() # Create the customized ArcPlot object: a2 a2 = ArcPlot(T, node_order='category', node_color='category') # Draw a2 to the screen a2.draw() # Display the plot plt.show()
matplotlib.pyplot has been imported for you as plt. INSTRUCTIONS 100XP Import ArcPlot from nxviz. Create an un-customized ArcPlot of T. To do this, use the ArcPlot() function with just T as the argument. Create another ArcPlot of T in which the nodes are ordered and colored by the 'category' keyword. You'll have to specify the node_order and node_color parameters to do this. For both plots, be sure to draw them to the screen and display them with plt.show(). ''' # Import necessary modules import matplotlib.pyplot as plt from nxviz import ArcPlot # Create the un-customized ArcPlot object: a a = ArcPlot(T) # Draw a to the screen a.draw() # Display the plot plt.show() # Create the customized ArcPlot object: a2 a2 = ArcPlot(T, node_order= 'category', node_color= 'category') # Draw a2 to the screen a2.draw() # Display the plot plt.show()