我需要使用以下数据构建一个不错的网络:
result_set = {('name1', 'job1'), ('name2', 'job2'), ('name3', 'job3'), ('name4', 'job4'), ('name5', 'job5'), ('name6', 'job6'), ('name7', 'job7'), ('name8', 'job8'), ('name9', 'job3'), ('name10', 'job6'), ('name11', 'job3'), ('name12', 'job1'), ('name13', 'job5'), ('name14', 'job9'), ('name15', 'job10'), ('name16', 'job6'), ('name17', 'job7'), ('name18', 'job11'), ('name19', 'job12'), ('name20', 'job13'), ('name21', 'job7'), ('name22', 'job14'), ('name23', 'job15'), ('name24', 'job7'), ('name25', 'job14'), ('name26', 'job9'), ('name27', 'job3'), ('name28', 'job16'), ('name29', 'job16'), ('name30', 'job1'), ('name31', 'job10'), ('name32', 'job9'), ('name33', 'job12'), ('name34', 'job5'), ('name35', 'job7'), ('name36', 'job3'), ('name37', 'job17'), ('name38', 'job3'), ('name39', 'job18'), ('name40', 'job16/job3'), ('name41', 'Il Foglio'), ('name42', 'job7'), ('name43', 'job19'), ('name44', 'job9'), ('name45', 'job20'), ('name46', 'job18'), ('name47', 'job21')}如您所见,名称是唯一的,但不是工作.因此,我想构建一个可以按作业显示名称集群的网络.
我使用以下代码完成了此操作,但我遇到了一些问题,无法以漂亮的格式进行可视化,而不会重叠标签并根据程度改变节点大小.
我的代码如下:
result = zip(names, jobs)# 将迭代器转换为集合result_set = 设置(结果)打印(结果集)G = nx.Graph()对于 result_set 中的 node_tuple:G.add_edges_from(result_set) # 评论后编辑nx.draw(G, with_labels=True)plt.show()能否请您看一下并告诉我如何相应地更改它以使其具有更好的可读性并根据节点的程度显示节点?
欢迎提出建议和意见.
解决方案您可以在
您还可以使用 node_color 参数根据节点是 job 还是 name 自定义节点颜色(我是猜测这不是真实情况,但它提供了如何进行的想法):
rcParams['figure.figsize'] = 14, 10pos = nx.spring_layout(G, scale=20, k=3/np.sqrt(G.order()))颜色 = [['lightgrey', 'lightblue'][node.startswith('job')]对于 G.nodes() 中的节点]d = dict(G.degree)nx.draw(G,位置,with_labels=真,节点列表=d,node_size=[d[k]*300 for k in d],节点颜色=颜色)I would need to build a nice network using the following data:
result_set = {('name1', 'job1'), ('name2', 'job2'), ('name3', 'job3'), ('name4', 'job4'), ('name5', 'job5'), ('name6', 'job6'), ('name7', 'job7'), ('name8', 'job8'), ('name9', 'job3'), ('name10', 'job6'), ('name11', 'job3'), ('name12', 'job1'), ('name13', 'job5'), ('name14', 'job9'), ('name15', 'job10'), ('name16', 'job6'), ('name17', 'job7'), ('name18', 'job11'), ('name19', 'job12'), ('name20', 'job13'), ('name21', 'job7'), ('name22', 'job14'), ('name23', 'job15'), ('name24', 'job7'), ('name25', 'job14'), ('name26', 'job9'), ('name27', 'job3'), ('name28', 'job16'), ('name29', 'job16'), ('name30', 'job1'), ('name31', 'job10'), ('name32', 'job9'), ('name33', 'job12'), ('name34', 'job5'), ('name35', 'job7'), ('name36', 'job3'), ('name37', 'job17'), ('name38', 'job3'), ('name39', 'job18'), ('name40', 'job16 / job3'), ('name41', 'Il Foglio'), ('name42', 'job7'), ('name43', 'job19'), ('name44', 'job9'), ('name45', 'job20'), ('name46', 'job18'), ('name47', 'job21')}Names are unique, but not jobs, as you can see. I would like, therefore, to build a network that can show clusters of names by jobs.
I did it using the following code, but I am having some issues to visualise in a nice format, without overlapping the labels and changing nodes size depending on degree.
My code is the following:
result = zip(names, jobs) # Converting itertor to set result_set = set(result) print(result_set) G = nx.Graph() for node_tuple in result_set: G.add_edges_from(result_set) # edited after comment nx.draw(G, with_labels=True) plt.show()Could you please have a look at it and tell me how I could change it accordingly to let it better readable and show nodes depending on nodes' degree?
Suggestions and comments are always welcomed.
解决方案You can use one of the many layout algorithms implemented in networkx in nx.drawing.layout to position the nodes in a way that makes the visualisation of the network easier. You can further adjust the distance between the nodes by setting k to an appropriate value.
Also you can set the node size to be proportional to the degree by building a dict from Graph.degree and setting the node_size in nx.draw accordingly and scaling it up to the desired size by applying a multiplicative factor. Here's an example using spring_layout:
from pylab import rcParams rcParams['figure.figsize'] = 14, 10 pos = nx.spring_layout(G, scale=20, k=3/np.sqrt(G.order())) d = dict(G.degree) nx.draw(G, pos, node_color='lightblue', with_labels=True, nodelist=d, node_size=[d[k]*300 for k in d])You could also custom the node colour according to whether a node is a job or a name using the node_color parameter (I'm guessing this is not the real case but it gives an idea of how to proceed):
rcParams['figure.figsize'] = 14, 10 pos = nx.spring_layout(G, scale=20, k=3/np.sqrt(G.order())) colors = [['lightgrey', 'lightblue'][node.startswith('job')] for node in G.nodes()] d = dict(G.degree) nx.draw(G, pos, with_labels=True, nodelist=d, node_size=[d[k]*300 for k in d], node_color=colors)更多推荐
使 networkx 情节看起来不错
发布评论