简体   繁体   English

NetworkX中的标签

[英]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. 我对6点钟左右的怪异半圆的标签特别感兴趣。

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 . 您可以通过分别绘制图形元素而不是像现在那样通过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. 在这种情况下,通过为draw_networkx_labels函数指定labels参数,您可以控制要显示的WHICH标签。 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. 另外,非常简单,由于您是通过plt.show()显示图形的,因此请注意底部的小放大类。 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. 如果足够,那么您甚至无需执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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