首页 > 解决方案 > 如何增加标签间距以避免networkx图中的标签重叠

问题描述

我想绘制一个图表,其中每个文档存在一个节点,然后是其中每个单词的其他节点。这样我想可视化出现在多个文档中的单词。

不幸的是,节点的标签重叠,因此它们经常不是很可读。

我试图增加和减少 k 变量,但这并没有真正的帮助。

我注意到如果再次绘制图表会发生变化,有时标签更具可读性,但它不是很有帮助,因为我有几个更大的图表,我需要确保它可以正常工作而不是依赖于重绘整个事情。

import networkx as nx
import matplotlib.pylab as plt
sentences = []
sentences.append("Jonathan likes to eat sweet cinnamon chocolate waffles.")
sentences.append("Jonathan also knows a really good recipe for baking cinnamon chocolate waffles.")
sentences.append("Some people prefer to eat savory waffles, especially if made by Jonathan.")
sentences.append("And some people do not like savory waffles at all.")

B=nx.from_dict_of_lists({0:[x for x in sentences[0].split()],1:[x for x in sentences[1].split()],2:[x for x in sentences[2].split()],
                         3:[x for x in sentences[3].split()]})

class_color=['blue','red','yellow','green']
node_color_array = []
nodesize = []

for node in B.nodes:
    set_nodesize=50
    color_to_add='white'
    for x in range(4):
        if(x==node):
            set_nodesize=200
            color_to_add =class_color[node]
    node_color_array.append(color_to_add)
    nodesize.append(set_nodesize)

plt.figure(5,figsize=(6,6), dpi=150, facecolor='w')

nx.draw(B,with_labels=True,node_color=node_color_array,node_size=nodesize,edge_color='grey')
plt.savefig('ExampleGraph.png')

绘制的图形如下所示:

在此处输入图像描述

有什么办法可以避免标签重叠?

标签: pythongraphnetworkx

解决方案


所以我没有找到一个干净的解决方案并制作了自己的解决方案,在那里我遍历所有节点并检查距离,如果它们太近,我通过将两个太靠近的节点推向相反的方向来调整位置:

import networkx as nx
import matplotlib.pylab as plt
sentences = []
sentences.append("Jonathan likes to eat sweet cinnamon chocolate waffles.")
sentences.append("Jonathan also knows a really good recipe for baking cinnamon chocolate waffles.")
sentences.append("Some people prefer to eat savory waffles, especially if made by Jonathan.")
sentences.append("And some people do not like savory waffles at all.")

B=nx.from_dict_of_lists({0:[x for x in sentences[0].split()],1:[x for x in sentences[1].split()],2:[x for x in sentences[2].split()],
                         3:[x for x in sentences[3].split()]})

class_color=['blue','red','yellow','green']
node_color_array = []
nodesize = []

for node in B.nodes:
    set_nodesize=50
    color_to_add='white'
    for x in range(4):
        if(x==node):
            set_nodesize=200
            color_to_add =class_color[node]
    node_color_array.append(color_to_add)
    nodesize.append(set_nodesize)

pos=nx.spring_layout(B)

#Push every node to the right so that coordinates are all positive
for node in B.node:
    pos[node]=[pos[node][0]+10,pos[node][1]+10]

#Check distances between nodes for number of iterations
for x in range(20):
    for nodex in B.node:
      for nodey in B.node:
          if(nodex != nodey):
              # if y distance is too small
              if(max(pos[nodex][1],pos[nodey][1])-min(pos[nodex][1],pos[nodey][1]) <0.6):
                  # check if also x distance is too small
                  if((max(pos[nodex][0],pos[nodey][0])-min(pos[nodex][0],pos[nodey][0])<0.3)):
                      #print(nodex,nodey)
                      if(pos[nodex][1] < pos[nodey][1]):
                          pos[nodex][1] = pos[nodex][1]-0.6
                          pos[nodey][1] = pos[nodey][1]+0.6
                      else:
                          pos[nodex][1] = pos[nodex][1]+0.6
                          pos[nodey][1] = pos[nodey][1]-0.6

plt.figure(5,figsize=(6,6), dpi=150, facecolor='w')

nx.draw(B,with_labels=True,node_color=node_color_array,node_size=nodesize,edge_color='grey')
plt.savefig('ExampleGraph.png')

我不是一个非常有经验的程序员,所以这不是最好的解决方案,需要针对不同的图表(距离等)进行调整,但它对我有用,总比没有好。 在此处输入图像描述


推荐阅读