plt.figure(figsize=(10, 10), facecolor='w', frameon=False)
pos = nx.graphviz_layout(G, prog='fdp')
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')
nx.draw_networkx_labels(G, pos, font_color='g')
label_ratio = 1.0 / 8.0
pos_labels = {}
for aNode in G.nodes():
    x, y = pos[aNode]
N = G[aNode]
cx = sum(map(lambda x: pos[x][0], N)) / len(pos)
cy = sum(map(lambda x: pos[x][1], N)) / len(pos)
slopeY = y - cy
slopeX = x - cx
pos_labels[aNode] = x + slopeX * label_ratio, y + slopeY * label_ratio
nx.draw_networkx_labels(G, pos=pos_labels, fontsize=2)
plt.show()
label nodes outside with minimum overlap with other nodes/edges in networkx