【问题标题】:Add padding to NetworkX graph in MatPlotLib在 MatPlotLib 中向 NetworkX 图形添加填充
【发布时间】:2021-03-29 22:50:33
【问题描述】:

我想用matplotlib 渲染一个networkx 图形。标签有点长(每个大约 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()

【问题讨论】:

    标签: python matplotlib networkx


    【解决方案1】:

    看起来像创建一个轴并设置一个边距就可以了:

    ax1 = plt.subplot(111)
    ax1.margins(0.3)           
    
    nx.draw(
      G,
      ax=ax1,
      with_labels=True,
      font_size=8,
      node_color='white',
    )
    

    【讨论】:

      猜你喜欢
      • 2017-06-28
      • 1970-01-01
      • 2019-07-20
      • 2012-08-07
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多