本文介绍了Networkx 绘图标签部分在框外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只需绘制一个非常简单的 4 节点网络,
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
我是用 networkx 绘图的新手.如何向左右添加边距以便显示完整标签?
I am new to drawing with networkx. How do I add margins to the left and right so the full labels can be shown?
推荐答案
不幸的是,似乎没有用于修复边距的自动化程序.您可以通过调用 plt.xlim(xmin,xmax)
手动调整边距.由于您知道节点位置 (pos
),因此您可以在每一侧添加额外的 25%:
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()
这篇关于Networkx 绘图标签部分在框外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!