问题
I want to plot a graph, where one node for each document exists and then other nodes for each word in it. This way I want to visualize words, that are occuring in multiple documents.
Unfortunately the labels for the nodes overlap, so that they aren't very readable oftentimes.
I have tried to increase and decrease the k variable, but it is not really helping.
I noticed that the graph changes if its plotted again, and sometimes the labels are more readable, but its not very helpful because I have several much bigger graphs, where I need to make sure that it works and not rely on redrawing the whole thing.
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')
The plotted graph looks like this:
Is there any way to avoid label overlaps?
回答1:
So I didn't find a clean solution and made my own, where I iterate through all nodes and check the distances and if they are too close, I adjust the position by pushing both nodes that are too close to one another into opposite directions:
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')
I'm not a very experienced programmer, so this is not the best solution and needs adjustments for different graphs (distances etc.) but it works for me and is better than nothing.
来源:https://stackoverflow.com/questions/58048415/how-to-increase-label-spacing-to-avoid-label-overlap-in-networkx-graph