0

I am planning to create a weighted square lattice for different sizes, and then label each edge with its associated weight. It's similar to this thread "How to load a weighed shapefile in networkX" that looks like intersection of different lines with different weights.

How can plot such a network and their weights? The weight can be for example just a random generator at this moment.

I have the following piece code from above page which does not work:

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np

N = 10
G=nx.grid_2d_graph(N,N)
weighted_G = str (np.random.rand()) 
pos = {v:v for v in weighted_G.nodes()}
labels = nx.get_edge_attributes(weighted_G,'weight')
nx.draw_networkx_nodes(weighted_G,pos, node_size=10,node_color='r')
nx.draw_networkx_edges(weighted_G, pos)
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)

# plt.xlim(577000, 579500) #This changes and is problem specific
# plt.ylim(9718500, 9721000) #This changes and is problem specific

plt.xlabel('X [m]')
plt.ylabel('Y [m]')
plt.show()
0

import networkx as nx

import matplotlib.pyplot as plt

import numpy as np

import math

import random

N = 20 G=nx.grid_2d_graph(N,N)

pos = dict((n, n) for n in G.nodes())

print "pos",pos

label = dict(((i, j), i + (N-1-j) * N) for i, j in G.nodes())

for u,v in list (G.edges):

G[u][v]['weight'] = int(random.random() * 10)

labels = nx.get_edge_attributes(G,'weight')

nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)

nx.draw_networkx(G, pos=pos, label=label)

print "labels", label

print "weights", labels

plt.axis('off')

plt.figure()

nx.draw_networkx(G, pos=pos, with_labels =True)

plt.show()

0

import networkx as nx

import matplotlib.pyplot as plt

import random

Set up a graph with random edges and weights

G = nx.barabasi_albert_graph(6, 2, seed= 3214562)

for u,v in list (G.edges):

G[u][v]['weight'] = int(random.random() * 10)

pos = nx.spring_layout(G)

nx.draw(G, pos)

nx.draw_networkx_edge_labels(G,pos)

plt.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.