我想绘制一个图,其中每个文档存在一个节点,然后其中每个单词存在另一个节点。 通过这种方式,我可以可视化出现在多个文档中的单词。
不幸的是,节点的标签重叠,因此它们通常不太可读。
我试图增加和减少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')
绘制的图形如下:
有什么办法可以避免标签重叠?
答案 0 :(得分:0)
所以我没有找到一个干净的解决方案,而是自己制作了一个解决方案,我在其中遍历所有节点并检查距离,如果距离太近,我通过将两个彼此距离太近的节点推入来调整位置相反的方向:
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')
我不是一个非常有经验的程序员,所以这不是最佳的解决方案,需要针对不同的图形(距离等)进行调整,但是它对我有用,总比没有好。