Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'
    
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading