def plot_graph(self, name, score): #Import matplotlib, a library used to produce graphic in python. import matplotlib.pyplot as plt from networkx import nx plt.figure(figsize=(7, 7)) # Adjust the window size plt.margins(.2, .2) # Adjust margins #We determine the node position: https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.drawing.layout.spring_layout.html position = nx.spring_layout(self.graph) nx.draw_networkx(self.graph, with_labels=True, pos=position) plt.axis('off') # Removing axis #Weights on edges : solution found on stackoverflow #https://stackoverflow.com/questions/28372127/add-edge-weights-to-plot-output-in-networkx labels = nx.get_edge_attributes(self.graph, 'weight') nx.draw_networkx_edge_labels(self.graph, pos=position, edge_labels=labels) plt.title( "fasta alignment graph according to a minimum alignment score of " + str(score)) plt.savefig( name + ".png") # Produce a png file with the graph annotated by default plt.show() nx.write_gexf( self.graph, name + ".gexf") # Produce a gexf file that can be annotated with gephi. return None
def plot(self, filename=None, with_capacity=False, dpi=300): """Plot network topology.""" pos = nx.spring_layout(self.graph) labels = nx.get_edge_attributes(self.graph, "capacity") nx.draw(self.graph, pos, with_labels=True) if with_capacity: nx.draw_networkx_edge_labels(self.graph, pos, edge_labels=labels) if filename: plt.savefig(filename, dpi=dpi)
def showGraph(G, W=None): n = len(G) E = [(i, j) for i in range(n) for j in G[i]] nxG = nx.Graph(E) pos = nx.spring_layout(nxG) nx.draw(nxG, pos, with_labels=True, node_color='yellow', node_size=500) if W is not None: labels = [W[i][k] for i in range(n) for k in range(len(G[i]))] for (i, j), w in zip(E, labels): nxG[i][j]['weight'] = w labels = nx.get_edge_attributes(nxG, 'weight') nx.draw_networkx_edge_labels(nxG, pos, edge_labels=labels) plt.show() return
def draw_small_graph(graph): """Draw the graph showing edge weight :param graph: graph object to draw """ graph_nx = nx.Graph() graph_nx.add_weighted_edges_from(graph.weighted_edges) labels = nx.get_edge_attributes(graph_nx, 'weight') pos = nx.spring_layout(graph_nx) nx.draw_networkx_edge_labels(graph_nx, pos=pos, edge_labels=labels) nx.draw(graph_nx, pos=pos, with_labels=True, node_size=10, node_color="skyblue", node_shape="o", alpha=0.5, linewidths=30) plt.title(graph.name) plt.show()
# print (a) # Drawing the graph node_pos = nx.spring_layout(G) edge_weight = nx.get_edge_attributes(G, 'weight') # Draw the nodes nx.draw_networkx(G, node_pos, node_color='grey', node_size=100) # Draw the edges nx.draw_networkx_edges(G, node_pos, edge_color='black') # Draw the edge labels nx.draw_networkx_edge_labels(G, node_pos, edge_color='red', edge_labels=edge_weight) # write to a file # nx.write_gml(G, "test_gml") # json_graph.node_link_data(G) # nx.write_edgelist(G,"test.edgeList") # print the adjacency list to terminal print("THE ADJACENCY LIST IS ") # try: # nx.write_adjlist(G, sys.stdout) # except TypeError: # nx.write_adjlist(G, sys.stdout.buffer) for a in [(n, nbrdict) for n, nbrdict in G.adjacency()]:
for i in range(no_of_cities): for j in range(no_of_cities): if i != j and distances[i][j] == 0: distances[i][j] = randint(1, 20) distances[j][i] = distances[i][j] # plot graph graph = networkx.from_numpy_matrix(numpy.array(distances)) print(distances) pos = nx.shell_layout(graph) nx.draw_networkx_nodes(graph, pos, node_size=80) nx.draw_networkx_labels(graph, pos, font_size=13, font_family='sans-serif') labels = nx.get_edge_attributes(graph, 'weight') nx.draw_networkx_edge_labels(graph, pos, edge_labels=labels, label_pos=0.61) nx.draw(graph, pos) plt.axis('off') plt.show() print() # assign ants to cities for i in range(no_of_ants): ants[i].visitedCitiesSet.append(ants[i].currentCity) # start process alphaBeta = [[0.5, 1.2], [2, 0.4], [1, 4], [1, 1]]