python - 如何增加标签间距以避免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')
绘制的图形如下所示:
有什么办法可以避免标签重叠?
解决方案
所以我没有找到一个干净的解决方案并制作了自己的解决方案,在那里我遍历所有节点并检查距离,如果它们太近,我通过将两个太靠近的节点推向相反的方向来调整位置:
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')
我不是一个非常有经验的程序员,所以这不是最好的解决方案,需要针对不同的图表(距离等)进行调整,但它对我有用,总比没有好。
推荐阅读
- java - 字符串日期时间转换为 LocalDateTime 对象
- c# - 在 emacs 中在同一行输入多个命令
- scala - 按分区从 Azure Blob 存储读取文件
- javascript - 如何限制 Google Cloud PubSub 以与使用 JavaScript 的不同服务的配额限制保持一致?
- html - Safari阅读器模式的奇怪行为
- node.js - 我的功能是在收到 params.finalemail 后不发送电子邮件
- javascript - React 中的键盘快捷键,react-hotkeys 库
- python - 如何在 python/matplotlib 中设置默认刻度参数?
- html - 居中滑块控制字形
- angular - 如何从单元测试中正确调用函数