pythonnetworkxpydot

Using pydot with networkx


I am trying to plot the following list as a graph with edge thickness proportional to the weights.

g_list=[('Alpha', 'Alpha', 7.06), ('Alpha', 'Bravo', 0.98), ('Alpha', 'Charlie', 0.0), ('Alpha', 'Delta', 0.0), ('Alpha', 'Echo', 1.57), ('Alpha', 'Foxtrot', 2.16), ('Alpha', 'Golf', 1.57), ('Alpha', 'Hotel', 0.39), ('Alpha', 'India', 0.0), ('Alpha', 'Juliet', 0.2), ('Alpha', 'Kilo', 0.59), ('Bravo', 'Alpha', 1.66), ('Bravo', 'Bravo', 8.54), ('Bravo', 'Charlie', 1.21), ('Bravo', 'Delta', 1.78), ('Bravo', 'Echo', 0.25), ('Bravo', 'Foxtrot', 0.76), ('Bravo', 'Golf', 1.66), ('Bravo', 'Hotel', 1.59), ('Bravo', 'India', 2.87), ('Bravo', 'Juliet', 1.72), ('Bravo', 'Kilo', 1.27), ('Charlie', 'Alpha', 1.0), ('Charlie', 'Bravo', 2.5), ('Charlie', 'Charlie', 7.0), ('Charlie', 'Delta', 5.0), ('Charlie', 'Echo', 0.0), ('Charlie', 'Foxtrot', 0.5), ('Charlie', 'Golf', 3.0), ('Charlie', 'Hotel', 0.0), ('Charlie', 'India', 0.5), ('Charlie', 'Juliet', 2.5), ('Charlie', 'Kilo', 1.5)]

The following code works but is not pretty

import networkx as nx

G=nx.Graph()
for i in range(len(g_list)):
    if((g_list[i][0] != g_list[i][1]) and (g_list[i][2] != 0.0)):
        G.add_edge(g_list[i][0],g_list[i][1],weight=g_list[i][2])
pos = nx.spring_layout(G)
for edge in G.edges(data='weight'):
    nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2])
nx.draw_networkx(G, pos, with_labels=True, arrows=True, arrowstyle='<-', alpha=1, node_color='#ffffff')
plt.axis('off')
plt.savefig('graph.jpg')

enter image description here

The sort of presentation I'm looking for can be obtained using pydot as folllows

G=nx.DiGraph()
for i in range(len(g_list)):
    if((g_list[i][0] != g_list[i][1]) and (g_list[i][2] != 0.0)):
        G.add_edge(g_list[i][1],g_list[i][0],weight=g_list[i][2])
p=nx.drawing.nx_pydot.to_pydot(G)
p.write_png('graph.png')

enter image description here

This is a better looking graph but when I try to add the variable thickness edges back using

pos = nx.spring_layout(G)
for edge in G.edges(data='weight'):
    nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2])
p=nx.drawing.nx_pydot.to_pydot(G)

I end up with the first graph again. Is there any way of combining the two approaches so that I get the layout of pydot and control over the drawing of the network edges? I have tried the following

pos=nx.nx_pydot.pydot_layout(G, prog='dot')
nx.draw_networkx(G, pos, with_labels=True, arrows=True, arrowstyle='<-', alpha=1, node_color='#ffffff')
for edge in G.edges(data='weight'):
    nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2])

with the following result, but still not the clear layout I get in the second graph.

enter image description here


Solution

  • If you want to use GraphViz's dot to render your graph with varying edge line width, you'll need to convert the weight to a penwidth attribute that GraphViz understands.

    I found using the actual weight made things way too thick, so here's something that takes the square root of the weight.

    Note you can use add_weighted_edges_from to convert your data in one fell swoop, too.

    import math
    
    import networkx as nx
    from networkx.drawing.nx_pydot import to_pydot
    
    g_list = [
        ("Alpha", "Alpha", 7.06),
        ("Alpha", "Bravo", 0.98),
        ("Alpha", "Charlie", 0.0),
        ("Alpha", "Delta", 0.0),
        ("Alpha", "Echo", 1.57),
        ("Alpha", "Foxtrot", 2.16),
        ("Alpha", "Golf", 1.57),
        ("Alpha", "Hotel", 0.39),
        ("Alpha", "India", 0.0),
        ("Alpha", "Juliet", 0.2),
        ("Alpha", "Kilo", 0.59),
        ("Bravo", "Alpha", 1.66),
        ("Bravo", "Bravo", 8.54),
        ("Bravo", "Charlie", 1.21),
        ("Bravo", "Delta", 1.78),
        ("Bravo", "Echo", 0.25),
        ("Bravo", "Foxtrot", 0.76),
        ("Bravo", "Golf", 1.66),
        ("Bravo", "Hotel", 1.59),
        ("Bravo", "India", 2.87),
        ("Bravo", "Juliet", 1.72),
        ("Bravo", "Kilo", 1.27),
        ("Charlie", "Alpha", 1.0),
        ("Charlie", "Bravo", 2.5),
        ("Charlie", "Charlie", 7.0),
        ("Charlie", "Delta", 5.0),
        ("Charlie", "Echo", 0.0),
        ("Charlie", "Foxtrot", 0.5),
        ("Charlie", "Golf", 3.0),
        ("Charlie", "Hotel", 0.0),
        ("Charlie", "India", 0.5),
        ("Charlie", "Juliet", 2.5),
        ("Charlie", "Kilo", 1.5),
    ]
    
    graph = nx.DiGraph()
    # Add edges, but reverse direction, remove self-loops, and remove zero-weight edges
    graph.add_weighted_edges_from([(b, a, w) for (a, b, w) in g_list if w > 0 and a != b])
    
    for edge in graph.edges().values():
        edge["penwidth"] = round(1 + math.sqrt(edge["weight"]), 2)
    
    p = to_pydot(graph)
    p.write_png("graph.png")
    

    The output is enter image description here