Re: [networkx-discuss] How to label nodes outside with minimum overlap with other nodes/edges

3,998 views
Skip to first unread message

Matthias Ekman

unread,
Aug 15, 2012, 4:13:26 PM8/15/12
to networkx...@googlegroups.com
There is no function for this in nx AFAIK, but you can have a look at this code snippet:

It does exactly what you want. It might also be worth considering to include it into nx as it seems an often requested feature.

Best,
Matthias

On 14 Aug 2012, at 19:01, New networkx user <fahi...@gmail.com> wrote:

Hi

I am trying to create a graph with node labels printed outside of nodes. I am able to generate 'offset' as shown below that solve the purpose. However, Sometimes the labels overlaps with edges (Which is undesirable as there are lots of empty spaces around nodes where the corresponding labels can be printed. See the attachment. ). I need to label these nodes in such a way that the labels does not overlap any edge or at least try to minimize overlap as much as possible. As you can see in the attached plot, 'bbbbb' and 'eeeee' overlaps with edges whereas  they could have been labeled below also.

I do see function 'label_pos' to position label on edge but was unsuccessful finding similar method for nodes. 

import networkx as nx
from networkx.utils import is_list_of_ints, flatten
import matplotlib.pyplot as plt

G
=nx.Graph()

G
= nx.complete_graph(5)
mapping
= {0:'aaaaaaa',1:'bbbbbbb',2:'ccccccc', 3:'dddddddd', 4:'eeeeeeeee'}
G
= nx.relabel_nodes(G,mapping)

plt
.figure(figsize=(10,10), facecolor="w", frameon=False)
pos
= nx.graphviz_layout(G, prog="fdp") #calculate position (x,y) coordinates
nx
.draw_networkx_nodes(G,pos,node_size=1200,node_shape='o',node_color='0.75')
nx
.draw_networkx_edges(G,pos, width=2,edge_color='b')


#for labeling outside the node
offset
=10
pos_labels
= {}
keys
= pos.keys()
for key in keys:
    x
, y = pos[key]
    pos_labels
[key] = (x, y+offset)
nx
.draw_networkx_labels(G,pos=pos_labels,fontsize=2)
plt
.show()

Is there any function in networkx that can deal with such situation? I googled for long with no success.

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/networkx-discuss/-/CKNFONFuadIJ.
To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.

Aric Hagberg

unread,
Aug 15, 2012, 5:38:54 PM8/15/12
to networkx...@googlegroups.com
On Wed, Aug 15, 2012 at 2:13 PM, Matthias Ekman
<matthia...@gmail.com> wrote:
> There is no function for this in nx AFAIK, but you can have a look at this
> code snippet:
> http://scikit-learn.org/stable/auto_examples/applications/plot_stock_market.html
>
> It does exactly what you want. It might also be worth considering to include
> it into nx as it seems an often requested feature.

I think what the OP wanted was labels that don't cross edges. So that
isn't quite the solution. I'd suggest looking at graphviz.org. You
can make a dot file from a networkx using nx.write_dot() (you'll need
the pydot or pygraphviz package).
Aric
Reply all
Reply to author
Forward
0 new messages