私は以前、ほとんどエッジの邪魔にならないようにすることを主なアイデアとして、同様のことを試みました。
エッジが直線であると仮定すると、これを実現するための単純で類似した方法が 2 つあります。
ノードの近傍のエッジがノード自体に対して作っている角度に基づいています。
近隣ノードの重心に基づく。
そのため、ノードから隣接するエッジに向かってエッジが形成される角度を見つけ、エッジの大部分からラベルを離して配置してみてください。または、ノードの近傍の重心を推定し、反対方向に沿ってラベルを配置します。
最初の解決策は、主にatan2関数の動作方法 (本質的にエッジ角度を決定する) が原因で、少し問題が生じる可能性がありますが、ラベルの配置に関してある程度の柔軟性を提供します。
2 番目の解決策は最も単純で、次のように機能します。
import networkx as nx
import matplotlib.pyplot as plt
#Build the graph
#Please note, the code here is as per the original post
G=nx.Graph()
G = nx.complete_graph(5)
mapping = {0:'aaaaaaa',1:'bbbbbbb',2:'ccccccc', 3:'dddddddd', 4:'eeeeeeeee'}
G = nx.relabel_nodes(G,mapping)
plt.figure(figsize=(10,10), facecolor="w", frameon=False)
#Get a graph layout
pos = nx.graphviz_layout(G, prog="fdp") #calculate position (x,y) coordinates
#Here is an alternative layout, please see below.
#pos = nx.layout.spring_layout(G)
nx.draw_networkx_nodes(G,pos,node_size=1200,node_shape='^',node_color='0.75')
nx.draw_networkx_edges(G,pos, width=2,edge_color='r')
#Show the original position of the labels using a Green colour.
nx.draw_networkx_labels(G,pos,font_color='g')
#Please note, the code below uses the original idea of re-calculating a dictionary of adjusted label positions per node.
label_ratio = 1.0/8.0
pos_labels = {}
#For each node in the Graph
for aNode in G.nodes():
#Get the node's position from the layout
x,y = pos[aNode]
#Get the node's neighbourhood
N = G[aNode]
#Find the centroid of the neighbourhood. The centroid is the average of the Neighbourhood's node's x and y coordinates respectively.
#Please note: This could be optimised further
cx = sum(map(lambda x:pos[x][0], N)) / len(pos)
cy = sum(map(lambda x:pos[x][1], N)) / len(pos)
#Get the centroid's 'direction' or 'slope'. That is, the direction TOWARDS the centroid FROM aNode.
slopeY = (y-cy)
slopeX = (x-cx)
#Position the label at some distance along this line. Here, the label is positioned at about 1/8th of the distance.
pos_labels[aNode] = (x+slopeX*label_ratio, y+slopeY*label_ratio)
#Finally, redraw the labels at their new position.
nx.draw_networkx_labels(G,pos=pos_labels,fontsize=2)
#Show the figure
plt.show()
これは、主にグラフの周辺にあるノードに対しては機能しますが、グラフの中心に向かって配置されたノードに対しては困難です。これは、セントロイドがエッジの大部分を回避する信頼できる方向を提供しないためです。
これがgraphvizのfdpレイアウトの出力です...

...そしてこれが networkx のspring layoutの出力です。

2 番目の図の緑と黒のラベルが近接していることに注意してください。基本的に、dddddd の近傍の重心は、ノードの実際の位置に比較的近いです。
より複雑なソリューションの場合は、ラベルがエッジと交差する場合にラベルの初期位置を調整するために、Wordle で使用されるアルゴリズムなど、より複雑なアルゴリズムを確認することをお勧めします。
お役に立てれば。