18

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

Community
  • 1
  • 1
user124784
  • 896
  • 1
  • 13
  • 22
  • Does the example in the networkx documentation help? http://networkx.github.io/documentation/latest/examples/drawing/edge_colormap.html Posting some code for what you tried would help us figure out what you need. – Aric Apr 09 '14 at 15:45
  • This is actually the example I was originally working from. The issue is that this example colours each edge one of the 20 shades in the range of the map. I want to colour edges based on their weight. I'm aware I could do this: https://networkx.github.io/documentation/latest/examples/drawing/weighted_graph.html But I'm worried it could take too long/be too difficult to implement. I will put some of the code I'm using up. – user124784 Apr 09 '14 at 16:07

1 Answers1

35

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')

enter image description here

Aric
  • 24,511
  • 5
  • 78
  • 77
  • Hey Aric, First of all thanks for the response, it looks like this is just what I need! However I'm going to ask for a little explanation here so I can adapt to what I need. So the edge_color parameter chooses the edges color from the map based on the numerical entry in the list? So if the weights are those entries will it scale by the numerical value? I mean will the larger values in my list be much darker than the other values? As a final question, when using a much larger graph will your code run efficiently enough or is there a better option? Thanks for your patience! – user124784 Apr 09 '14 at 18:57
  • The colormap is applied to the edge_color values. You can choose a colormap that has the right high/low coloring (e.g. try Blues or Blues_r). You can probably make drawings of up to a few thousand edges fairly quickly. More edges than that and you are going to get a "hairball" with this approach. – Aric Apr 09 '14 at 21:19
  • My main graph is ~1M edges, but some of the subgraphs I want to investigate are in this sort of range so this should help greatly with that! Thanks for your help. – user124784 Apr 09 '14 at 21:26