簡體   English   中英

如何增加 label 間距以避免 label 在 networkx 圖中重疊

[英]How to increase label spacing to avoid label overlap in networkx graph

我想 plot 一個圖,其中每個文檔存在一個節點,然后每個單詞都有其他節點。 這樣我想可視化出現在多個文檔中的單詞。

不幸的是,節點的標簽重疊,因此它們經常不是很可讀。

我試圖增加和減少 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')

繪制的圖形如下所示:

在此處輸入圖像描述

有什么辦法可以避免 label 重疊?

所以我沒有找到一個干凈的解決方案並制作了自己的解決方案,在那里我遍歷所有節點並檢查距離,如果它們太近,我通過將兩個太靠近的節點推向相反的方向來調整 position:

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')

我不是一個非常有經驗的程序員,所以這不是最好的解決方案,需要針對不同的圖表(距離等)進行調整,但它對我有用,總比沒有好。 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM