在 MatPlotLib 中向 NetworkX 图形添加填充

时间:2021-03-29 22:50:33

标签: python matplotlib networkx

我想用 networkx 渲染一个 matplotlib 图。标签有点长(每个大约 10 - 50 个字符),而且它们往往会被切断,使我无法阅读它们。有没有办法让 matplotlib “缩小”最终渲染,以便我可以阅读所有文本?

下面是我的代码以及渲染图。

import networkx as nx
import matplotlib.pyplot as plt


G = nx.Graph()
root_node = 'this is the root node'
G.add_node(root_node)
for other_node in [
  'test is an adjacent node',
  'test is another adjacent node',
  'test is yet another adjacent node',
  'test is the fourth adjacent node',
]:
  G.add_node(other_node)
  G.add_edge(root_node, other_node)
nx.draw(G, with_labels=True, font_size=8, node_color='white')
plt.show()

graph rendering

1 个答案:

答案 0 :(得分:0)

看起来像创建一个轴并在其上设置边距做到了:

ax1 = plt.subplot(111)
ax1.margins(0.3)           

nx.draw(
  G,
  ax=ax1,
  with_labels=True,
  font_size=8,
  node_color='white',
)

layout