I am using python networkx lib draw a node relation graph. Code like this:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([("leg", 'body'),('body', 'head'),('body','arm'),('arm','hand')])
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos)
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G,pos)
plt.show()
Everything is fine. The figure is:
However, I'd like to put the label outside of the node. Then I adjust the position of labels. code is:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([("leg", 'body'),('body', 'head'),('body','arm'),('arm','hand')])
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos)
nx.draw_networkx_edges(G,pos)
# nx.draw_networkx_labels(G, pos)
nx.draw_networkx_labels(G, pos = {k:([v[0], v[1]+0.1]) for k,v in pos.items()})
plt.show()
Then the figure is:
The question is, the label do not display totally, but exceed the boundary. so how can I display the labels normally? thanks.


