How to draw networkx graph with edge labels

25,945 views
Skip to first unread message

Dushyant

unread,
Aug 11, 2010, 6:01:27 AM8/11/10
to networkx-discuss
I have created a graph g with weights assigned to each edge. How do I
draw this graph so that the edge weights are displayed. Kindly if
possible provide the code.


G = nx.Graph()
G.add_edge(1, 2, weight=3)
G.add_edge(2, 3, weight=5)
networkx.draw(G)

Aric Hagberg

unread,
Aug 11, 2010, 8:49:03 AM8/11/10
to networkx...@googlegroups.com

import networkx as nx
import pylab


G = nx.Graph()
G.add_edge(1, 2, weight=3)
G.add_edge(2, 3, weight=5)

pos=nx.spring_layout(G)

# version 1
pylab.figure(1)
nx.draw(G,pos)
# use default edge labels
nx.draw_networkx_edge_labels(G,pos)

# version 2
pylab.figure(2)
nx.draw(G,pos)
# specifiy edge labels explicitly
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)

# show graphs
pylab.show()

Aric

Dushyant

unread,
Aug 11, 2010, 9:53:24 AM8/11/10
to networkx-discuss
Thanks Aric!
Version 2 was exactly what I was looking for

On Aug 11, 2:49 pm, Aric Hagberg <ahagb...@gmail.com> wrote:

krumpelstiltskin

unread,
May 7, 2013, 4:46:51 PM5/7/13
to networkx...@googlegroups.com
Thanks for the post.
I noticed that this doesn't support directed graphs with cycles of length 2.
In that case, a single edge is drawn with two arrow heads, but only one of the 2 labels.

Do you know any way to fix this?

kms

Johannes Castner

unread,
Jul 16, 2013, 3:56:49 PM7/16/13
to networkx...@googlegroups.com
Version two is exactly what I needed, thank you!

If you also know how to change the size of the edge labels and/or how to change the arrows on a DiGraph, so that they look more like arrows, rather than shovels, please pass that knowledge along!

Johannes

Reshma Okkath Kavil

unread,
Feb 6, 2014, 1:42:09 AM2/6/14
to networkx...@googlegroups.com
Hi
Can u please help me in creating directed graphs having 2 edges between a pair of nodes?
I have created such a graph but, am not able to distinguish between the two edges.
i.e, an edge is displayed between those nodes with arrows at both ends.
Also, while labeling only one label is displayed. Kindly help me to fix this issue.

Aric Hagberg

unread,
Feb 6, 2014, 9:36:03 AM2/6/14
to networkx...@googlegroups.com
Hi,
NetworkX drawing with Matplotlib doesn't support showing such parallel edges.
You can do it with Graphviz. See here for an example
http://stackoverflow.com/questions/14943439/how-to-draw-multigraph-in-networkx-using-matplotlib-or-graphviz

Aric
> --
> You received this message because you are subscribed to the Google Groups
> "networkx-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to networkx-discu...@googlegroups.com.
> To post to this group, send email to networkx...@googlegroups.com.
> Visit this group at http://groups.google.com/group/networkx-discuss.
> For more options, visit https://groups.google.com/groups/opt_out.

Reshma Okkath Kavil

unread,
Mar 26, 2014, 1:09:10 PM3/26/14
to networkx...@googlegroups.com
Thankyou Aric for replying.

arun mukundan

unread,
May 24, 2015, 8:21:54 AM5/24/15
to networkx...@googlegroups.com
Hey Aric,
I tried to do what you said, but the output is showing me all attributes of the edges rather than just weight, as below

my edge_labels looks like 

{('arandjelovic14a', 'arandjelovic14b'): 19, ('arandjelovic14a', 'arandjelovic14'): 25, ('arandjelovic14b', 'arandjelovic14'): 10}

And my code is as follows:
nx.draw(G, pos)
#edge_labels = nx.get_edge_attributes(G,'weight')
edge_labels=dict([((u,v,),d['weight'])
             for u,v,d in G.edges(data=True)])
print edge_labels
nx.draw_networkx_edge_labels(G, pos, labels = edge_labels)
plt.show()

Any ideas where I'm going wrong?

Jamie Morton

unread,
Oct 7, 2015, 4:59:34 PM10/7/15
to networkx-discuss
nx.draw_networkx_edge_labels(G, pos, labels = edge_labels) is incorrect

It should be nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_labels)
Reply all
Reply to author
Forward
0 new messages