5
votes

I a using NetworkX for a network analysis in python. I determine the weight for every edge and add that edge to the graph in the following way:

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


airports = ['ATL','LAX','ORD']
weights  = [500,200,150] #Note that in my real code I I calculated these weights, they are not provided
G = nx.Graph()
G.add_nodes_from(airports)

weightlst = []
airports_pos = []
checked_airports = []

i = 0
for airport1 in airports:
    for airport2 in airports:
        if airport1 != airport2 and  checked_airports.count([airport1,airport2])==0 and checked_airports.count([airport2,airport1])==0:
            weightedge = weights[i]
            weightlst.append(weightedge)
            weightedge = weightedge*0.0020+0.5
            G.add_edge(airport1, airport2, weight=weightedge)
    checked_airports.append([airport1,airport2])
    i = i + 1

For context, the weight of each edge indicates how many flights occur between two airports, and my issue is that it is unclear which 'routes' are 'busiest' because the irrelevant edges are drawn over the relevant ones. I wish to draw the edges with the highest weight last so that it is clear which are the 'busiest flight routes' in the network.

3
Why do you want to change the order in which edges are added? Do you want your graph to be able to have multiple edges per pair of nodes?Alex Hall
I have a network consisting of 30 nodes and therefore 435 edges. To be able to visualise this network in a proper way, I want the edges with a higher weight to be drawn on top of the edges with a smaller weight so they can be clearly seen (note that I also make use a colour gradient, that is: the higher the weight, the darker the edge colour).rational-pi

3 Answers

8
votes

Use:

edges=sorted(G.edges(data=True), key=lambda t: t[2].get('weight', 1))
0
votes

Put all the edge data in a list and then sort it with a custom key function (this is very easy to look up). NetworkX probably doesn't have any functionality to do what you want because it doesn't need to.

0
votes

Adding to the selected answer since it can feel somewhat complicated... (can't comment due to insufficient reputation and edit request rejected ¯\(ツ)/¯)

G.edges returns a list of edges added to graph G (each edge represented by a tuple of two elements: the start node and the end node), setting its data attribute to True includes the weight which was set for each edge in that list as the third element of the tuple as a dictionary, where the weight's key is the attribute name that has been set for the edge weights (which is by default 'weight').

The lambda function evaluates each tuple t and compares them via the third element (which is the dictionary) by fetching the value corresponding to the key 'weight', defaulting to 1 if that key is not found.

It is to be noted that if you've chosen an attribute name other than 'weight' for the edge weight, the above line of code will not work and will not show any errors either, it will just do nothing; for it will default to returning 1 in all cases as it can't find the key and hence effectively sort nothing. An edge can have different attributes of different interpretations, so make sure to choose the key based on which you'd like to sort.