I have only found something similar to what I want here:
Coloring networkx edges based on weight
However I can't seem to apply this to my problem. I have a graph with weighted edges, but the weights aren't unique (so there are like 15 edges with weight 1). I want to colour my edges based on the weight they have, the lower the weight the lighter the colour.
I tried to apply the method suggested in the above question, but from what I understand this requires the weights to be unique on each edge?
So far I've produced a list in ascending order of the different edge weights and wanted to use this to classify the possible edge colours. I'm trying to avoid drawing the edges by weight as I may need to draw a very large graph in the future with a huge range of weights on the edges.
If it's unclear let me know in comments and I'll give more specific info.
Thanks!
EDIT: def draw_graph(target): nlist = [target]+G.neighbors(target) H=nx.subgraph(G, nlist) n=H.number_of_edges() colours = range(n) labels,weights = colour_and_label_edges(H)
pos = nx.spring_layout(H)
nx.draw(H, pos, node_color='#A0CBE2',edge_color=colours, node_size=100, edge_cmap=plt.cm.Blues, width=0.5, with_labels=False)
nx.draw_networkx_edge_labels(H, pos, edge_labels=labels)
plt.savefig("Graphs/edge_colormap_%s.png" % target) # save as png
plt.show() # display
pass
def colour_and_label_edges(graph):
d={}
for (u,v) in graph.edges():
d[u,v]=graph[u][v]['weight']
temp=[]
for val in d.values():
if val not in temp:
temp.append(val)
weights = sorted(temp,key=int)
return d, weights
The above code is incomplete, but the idea is the function gives me a list of the weights, as so:
[1, 2, 3, 4, 5, 6, 9, 10, 16, 21, 47, 89, 124, 134, 224]
I then want to use this list to assign each weight a colour, the higher the weight the darker the colour. (I've used a very small subgraph for this example relative to the data set). Hope that clears it up a little :S
Add all the edges in ebunch as weighted edges with specified weights. Each edge given in the list or container will be added to the graph. The edges must be given as 3-tuples (u,v,w) where w is a number.
You can use the G. edges() or G. edges_iter() methods to loop over all of the graph edges.
You can use the edge weights and a colormap to draw them. You might want t a different colormap from the one below.
import matplotlib.pyplot as plt
import networkx as nx
import random
G = nx.gnp_random_graph(10,0.3)
for u,v,d in G.edges(data=True):
d['weight'] = random.random()
edges,weights = zip(*nx.get_edge_attributes(G,'weight').items())
pos = nx.spring_layout(G)
nx.draw(G, pos, node_color='b', edgelist=edges, edge_color=weights, width=10.0, edge_cmap=plt.cm.Blues)
plt.savefig('edges.png')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With