Adding weighted directed edges to the networkx plot with an arrow from source to destination

Question:

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import pylab
plt.rcParams['figure.figsize'] = [10, 10]
G = nx.DiGraph()

G.add_edges_from([('A', 'B'),('C','D'),('G','D')], weight=1)
G.add_edges_from([('D','A'),('D','E'),('B','D'),('D','E')], weight=2)
G.add_edges_from([('B','C'),('E','F')], weight=3)
G.add_edges_from([('C','F')], weight=4)



nodeopt = {
    'node_color': 'white',
    'node_size': 3500,
    'node_shape': 'o',
    'linewidths': 1,
    'edgecolors' : 'black'
}

label_opt = {
    'font_size': 13,
    'font_color' : 'black',
}

edge_opt = {
    'width': 3,
    'edge_color' : 'black',
    'arrowstyle' : '-|>',
    'arrows' : True,
    'arrowsize' : 12,
    'connectionstyle' : 'arc3,rad=0.2'
    
}


##FOR PRINTING WEIGHTS ON THE EDGES
edge_labels=dict([((u,v,),d['weight'])
                 for u,v,d in G.edges(data=True)])

## EDIT THE EDGE COLORS 
red_edges = []
edge_colors = ['black' if not edge in red_edges else 'red' for edge in G.edges()]
#red_edges = [('C','D'),('D','A')]
#edge_colors = ['black' if not edge in red_edges else 'red' for edge in G.edges()]

## FOR CREATING LABELS OF THE NODES 
node_labels = {node:node for node in G.nodes()}; 


pos=nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos,**nodeopt)

nx.draw_networkx_labels(G, pos, labels=node_labels, **label_opt)

nx.draw_networkx_edges(G, pos, **edge_opt) 

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

pylab.show()

Hello above is the code I am working. I was able to get the weighted edges but for some reason my graphs are not shown as directed despite using "nx.DiGraph()", would anyone be able to help?

For the case of G.add_edges_from([(‘C’,’F’)], weight=4), I want an arrow from node C to F with a weight 4

Asked By: Cindy Burker

||

Answers:

As I suggested in my comments, the nodes are so big that they’re covering the arrowheads. The way to accommodate is to use the node_size argument when drawing the edges, not just the nodes. In other words, you can get the arrows back if you add a line to edge_opt, as in

edge_opt = {
    'node_size': 3500,
    'width': 1,
    'edge_color' : 'black',
    'arrowstyle' : '-|>',
    'arrows' : True,
    'arrowsize' : 12,
    'connectionstyle' : 'arc3,rad=0.2'
    
}
Answered By: Ben Grossmann

I solved it by referring to the library manual:
On the draw_networkx_edges function, there is a parameter to specify your node size. when you plug the same value into both, it fixes it!

  nodeopt = {
    'node_size' : 1400,
    'node_color': 'white',
    'node_shape': 'o',
    'linewidths': 1,
    'edgecolors' : 'black'
}

label_opt = {
    'font_size': 13,
    'font_color' : 'black',
}

edge_opt = {
    'width': 3,
    'edge_color' : 'black',
    'arrowstyle' : '-|>',
    'arrows' : True,
    'arrowsize' : 22,
    'connectionstyle' : 'arc3,rad=0.2',
    'node_size' : 1400
    
}
Answered By: Cindy Burker