示例#1
0
def draw(graph: Graph, components):
    """Draw the graph showing edge weight

    :param graph: graph object to draw
    :param components: tuple of sets, where each set represents a set of nodes
    """

    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)
    community_map = get_community_map(components)

    nx.draw_networkx(graph_nx,
                     pos=pos,
                     with_labels=False,
                     node_size=200,
                     node_shape="o",
                     node_color=list(community_map.values()))

    plt.get_cmap()
    plt.axis('off')
    plt.title(graph.name)
    plt.show()
示例#2
0
    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
示例#3
0
 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)
示例#4
0
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
示例#5
0
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()
示例#6
0
print("Configuration model")
G = nx.configuration_model(z)  # configuration model
degree_sequence = [d for n, d in G.degree()]  # degree sequence
print("Degree sequence %s" % degree_sequence)
print("max degree %s", max(degree_sequence))
print("min degree %s", min(degree_sequence))
print("Connected: %s", nx.is_connected(G))
print("Degree histogram")
hist = {}
for d in degree_sequence:
    if d in hist:
        hist[d] += 1
    else:
        hist[d] = 1
print("degree #nodes")
for d in hist:
    print('%d %d' % (d, hist[d]))

edges = list(G.edges)
block_delay = {}
# ea = nx.edge_betweenness_centrality(G, normalized=False) # get a dictionary of edges: value. easier to modify
block_delay_distribution = list(np.random.normal(200, 400,
                                                 G.number_of_edges()))
for e in edges:
    block_delay[e] = max(0, block_delay_distribution.pop())
# print(block_delay)
nx.set_edge_attributes(G, block_delay, 'block_delay')
print(nx.get_edge_attributes(G, 'block_delay'))
# nx.draw(G)
# plt.show()
for v in nx.nodes(G):
    print('%s %d %f' % (v, nx.degree(G, v), nx.clustering(G, v)))

# G.add_edge(0, 3, weight=4)
for e in G.edges:
    a = e[0]
    b = e[1]

    w = randint(1, 1)
    G.add_edge(int(e[0]), int(e[1]), weight=w)
# 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")
示例#8
0
            nx.draw(g,
                    pos,
                    node_color='g',
                    node_size=100,
                    font_size=10,
                    edge_color='k',
                    width=1,
                    with_labels=True)
            # plt.title('Source graph: '+str(i))
            plt.title('Subject {}:  SC'.format(i))
            # plt.show()

            # plt.figure(figsize=(w, h))
            plt.subplot(132, aspect=1.0)
            g = nx.from_numpy_array(tfc)
            edges, weights = zip(*nx.get_edge_attributes(g, 'weight').items())
            # weights[weights>=0] = 1
            # weights[weights<=0] = -1
            nx.draw(g,
                    pos,
                    node_color='g',
                    node_size=100,
                    font_size=10,
                    edge_color=weights,
                    width=2,
                    with_labels=True)
            # nx.draw(g, pos, node_color='g', node_size=100, font_size=10, edge_color='k', width=1, with_labels=True)
            plt.title('Subject {}: Empirical FC'.format(i))
            # plt.show()

            # plt.figure(figsize=(w, h))
示例#9
0
graph = nx.read_weighted_edgelist("8_0.01diamter3_newtest.weighted.edgelist")

# some properties
print("node degree clustering")
for v in nx.nodes(graph):
    print('%s %d %f' % (v, nx.degree(graph, v), nx.clustering(graph, v)))

# print the adjacency list to terminal
try:
    nx.write_adjlist(graph, sys.stdout)
except TypeError:
    nx.write_adjlist(graph, sys.stdout.buffer)

node_pos = nx.spring_layout(graph)

edge_weight = nx.get_edge_attributes(graph, 'weight')
# Draw the nodes
nx.draw_networkx(graph, node_pos, node_color='grey', node_size=100)

# Draw the edges
nx.draw_networkx_edges(graph, node_pos, edge_color='black')

# Draw the edge labels
nx.draw_networkx_edge_labels(graph,
                             node_pos,
                             edge_color='red',
                             edge_labels=edge_weight)
print(edge_weight)
print(graph)
print(graph.nodes())
print("THE GRAPH")
ants = [Ant(randint(0, no_of_cities - 1), [], 0) for i in range(no_of_ants)]

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]]