Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

python - Networkx drawing label partially outside the box

Just draw a very simple 4-node network,

import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
node1 = "116.251.211.248"
node2 = "5.79.100.165"
node3 = "http.anturis.com"
node4 = "s411993.ip-37-187-141.eu"
G.add_node(node1)
G.add_node(node2)
G.add_node(node3)
G.add_node(node4)
G.add_weighted_edges_from([(node1, node2, 0.742345), (node1, node3, 0.916954), (node1, node4, 0.662011), (node1, node4, 0.818537), (node2, node3, 0.947824), (node2, node4, 0.800774), (node3, node4, 0.928537)])
pos=nx.shell_layout(G)
nx.draw(G, pos, with_labels=True)
plt.show()

My problem is the edge labels fall partially outside of the box

enter image description here

I am new to drawing with networkx. How do I add margins to the left and right so the full labels can be shown?

question from:https://stackoverflow.com/questions/65652083/how-to-display-the-full-naming-of-the-networkx-nodes-when-ploting-the-graph-with

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unfortunately, there does not seem to be an automated procedure for fixing the margins. You can adjust the margins by hand by calling plt.xlim(xmin,xmax). Since you know the node positions (pos), you can add, say, an extra 25% on each side:

#Your code here....
nx.draw(G, pos, with_labels=True)
x_values, y_values = zip(*pos.values())
x_max = max(x_values)
x_min = min(x_values)
x_margin = (x_max - x_min) * 0.25
plt.xlim(x_min - x_margin, x_max + x_margin)
plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...