def test_node_size(): # add size as attribute and fill with random numbers nodes = G.nodes() for u in nodes: G.node[u]["score"] = random() # also extract list for testing scores = [G.nodes[u]["score"] for u in nodes] # add color as property a = ArcPlot(G, node_size="score") assert a.node_sizes == scores # add types as list a = ArcPlot(G, node_size=scores) assert a.node_sizes == scores
def test_edge_widths(): # add weight as attribute and fill with random numbers edges = G.edges() for u, v in edges: G[u][v]["weight"] = random() # also extract list for testing weights = [G[u][v]["weight"] for u, v in edges] # add weights as proptery c = CircosPlot(G, edge_width="weight") assert c.edge_widths == weights a = ArcPlot(G, edge_width="weight") assert a.edge_widths == weights # add weights as list c = CircosPlot(G, edge_width=weights) assert c.edge_widths == weights a = ArcPlot(G, edge_width=weights) assert a.edge_widths == weights
def test_edge_color(): # add color as attribute and fill with random numbers edges = G.edges() for u, v in edges: G[u][v]["type"] = "a" if random() < 0.5 else "b" # also extract list for testing types = [G[u][v]["type"] for u, v in edges] # add color as property c = CircosPlot(G, edge_color="type") assert corresponding_lists(c.edge_colors, types) a = ArcPlot(G, edge_color="type") assert corresponding_lists(a.edge_colors, types)
# Create the CircosPlot object: c c = CircosPlot(T) # Draw c to the screen c.draw() # Display the plot 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
def test_arc_plot(): a = ArcPlot(G) # noqa: F841 diff = diff_plots(a, "arc.png", baseline_dir, result_dir) assert diff is None
# Create the customized MatrixPlot object: h h = MatrixPlot(graph=largest_ccs, node_grouping='grouping') print(' Draw the MatrixPlot ') h.draw() plt.savefig('MatrixPlot.png') plt.clf() # 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') print(' Draw the ArcPlot to the screen ') a.draw() plt.savefig('ArcPlot.png') # 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) # Create the CircosPlot object: c c = CircosPlot(G, node_order='degree', node_grouping='grouping',
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()
Following on what you've learned about the nxviz API, now try making an ArcPlot of the network. Two keyword arguments that you will try here are node_order='keyX' and node_color='keyX', in which you specify a key in the node metadata dictionary to color and order the nodes by. 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()
# Draw c to the screen c.draw() # Display the plot plt.show() # Visualizing using Arc plots # Following on what you've learned about the nxviz API, now try making an ArcPlot of the network. Two keyword arguments that you will try here are node_order='keyX' and node_color='keyX', in which you specify a key in the node metadata dictionary to color and order the nodes by. # matplotlib.pyplot has been imported for you as plt. # 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(node_order='category',node_color='category',graph = T) # Draw a2 to the screen a2.draw() # Display the plot plt.show()
node_labels=True, node_order='Y', font_size=32, node_color='C', figsize=(8, 8), font_weight='bold') # This creates a circosplot object Mat. Mat.draw() plt.savefig('./figures/Cerco_credit_transaction.png') plt.show() ## Arcplot from nxviz import ArcPlot Mat = ArcPlot(G, node_labels=True, font_size=25, node_order='Y', node_color='C', figsize=(8, 8)) Mat.draw() plt.savefig('./figures/Arc_credit_transaction.png') plt.show() # Centrality Measures ## Degree Centrality pos = node_pos draw_graph(G, pos, nx.in_degree_centrality(G), 'In-degree Centrality', './figures/indegree_credit_transaction.png') draw_graph(G, pos, nx.out_degree_centrality(G), 'Out-degree Centrality', './figures/outdegree_credit_transaction.png')
# Draw c to the screen c.draw() # Display the plot plt.show() -------------------------------------------------- # Exercise_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()
def test_arc_plot(): a = ArcPlot(G) # noqa: F841
def test_answer(mf_counts): assert mf_counts['female'] == 17 assert mf_counts['male'] == 12 test_answer(mf_count) from nxviz import MatrixPlot m = MatrixPlot(g) m.draw() plt.show() from nxviz import ArcPlot a = ArcPlot(g) a.draw() from nxviz import CircosPlot c = CircosPlot(g) c.draw() plt.show() # plt.savefig('images/seventh.png', dpi=300) #%% hiveplot #%% #%% DG = nx.DiGraph() # make a directed graph (digraph) DG.add_nodes_from(["S", "A", "B", "C", "D", "E", "T"]) # add nodes
from nxviz import ArcPlot from nxviz import CircosPlot # plt.style.use('ggplot') from itertools import combinations T = nx.erdos_renyi_graph(n=45, p=0.8, seed=456) c = CircosPlot(T) # Draw c to the screen c.draw() plt.pause(2) plt.clf() # Create the un-customized ArcPlot object: a a = ArcPlot(T) # Draw a to the screen a.draw() # Display the plot plt.pause(2) plt.close() def nodes_with_m_nbrs(G, m): """ Returns all nodes in graph G that have m neighbors. """ nodes = set()
# Create the CircosPlot object: c 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()
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()
def test_arc_plot(): a = ArcPlot(G)