简体   繁体   中英

Labels in NetworkX

I have a fairly complicated network.

    Number of nodes: 2435
    Number of edges: 7497
    Average degree:   6.1577

I want to add labels to network, which I have managed to do, however because of the number of nodes it doesn't look great.

带有标签的网络 Is there anyway to maybe get the every fifth label? Which may make it a bit more readable. I'd be particularly interested in the labels for the weird semi-circle around 6 o'clock.

Alternatively is there a way to plot the nodes and related edges that have the highest degree of centrality.

Another method, to make an interactive graph allowing to zoom, which I have no idea how to do.

Code:

cast_1_tup = castdf[['cast_0','cast_1']].apply(tuple, axis=1)
cast_2_tup = castdf[['cast_0','cast_2']].apply(tuple, axis=1)
cast_3_tup = castdf[['cast_0','cast_3']].apply(tuple, axis=1)
cast_4_tup = castdf[['cast_0','cast_4']].apply(tuple, axis=1)
cast_5_tup = castdf[['cast_1','cast_2']].apply(tuple, axis=1)
cast_6_tup = castdf[['cast_1','cast_3']].apply(tuple, axis=1)
cast_7_tup = castdf[['cast_1','cast_4']].apply(tuple, axis=1)
cast_8_tup = castdf[['cast_2','cast_3']].apply(tuple, axis=1)
cast_9_tup = castdf[['cast_2','cast_4']].apply(tuple, axis=1)
cast_10_tup = castdf[['cast_3','cast_4']].apply(tuple, axis=1)

G = nx.Graph()
G.add_edges_from(cast_1_tup)
G.add_edges_from(cast_2_tup)
G.add_edges_from(cast_3_tup)
G.add_edges_from(cast_4_tup)
G.add_edges_from(cast_5_tup)
G.add_edges_from(cast_6_tup)
G.add_edges_from(cast_7_tup)
G.add_edges_from(cast_8_tup)
G.add_edges_from(cast_9_tup)
G.add_edges_from(cast_10_tup)

# write in UTF-8 encoding
fh = open('edgelist.utf-8', 'wb')
fh.write('# -*- coding: utf-8 -*-\n'.encode('utf-8'))  # encoding hint for emacs
nx.write_multiline_adjlist(G, fh, delimiter=',', encoding='utf-8')

# read and store in UTF-8
fh = open('edgelist.utf-8', 'rb')
H = nx.read_multiline_adjlist(fh, delimiter=',', encoding='utf-8')

plt.figure(figsize=(40,40))
plt.axis('off')
pos = nx.spring_layout(G, scale =2)
nx.draw_networkx(G, pos, cmap = plt.get_cmap('jet'), node_colour = values , node_size=80, with_labels=False)
for p in pos:  # raise text positions
    pos[p][1] += 0.04
nx.draw_networkx_labels(G, pos)
plt.show()

You can achieve a few of the things you are asking for by drawing the elements of the graph separately, rather than all in one go, as you are doing now via nx.draw_networkx .

Specifically, try dividing the drawing into three parts:

nx.draw_networkx_nodes(G, pos=pos)
nx.draw_networkx_edges(G, pos=pos)
nx.draw_networkx_labels(G, pos=pos, labels=l)

In this case, by specifying the labels parameter to the draw_networkx_labels function, you can be in control of WHICH labels you want to display. This should be a dictionary keyed by actual label value, and it can map to whatever you want -- either to itself, or to something else. And seems like you don't have to specify them all, so you can programmatically determine what goes into that dictionary and omit the rest:

l = {'node-1':'node-1'}

So this should print only one label and not the rest.

Also, very simply, since you are showing your graph via plt.show(), note the little magnifying class at the bottom. That allows you to zoom in as many times as you want into a very detailed level. If that's sufficient, then you don't even have to do anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM