【问题标题】:networkx graph display using matplotlib- missing labels使用matplotlib-缺少标签的networkx图形显示
【发布时间】:2016-06-08 14:17:21
【问题描述】:

我正在编写一个程序,它为特定的输入字符串生成可满足的模型(连接图)。这里的细节并不重要,但主要问题是每个节点都有一个标签,而这个标签可能很长。因此,发生的情况是它不适合导致显示所有节点但部分标签显示的图形......此外,显示的图形不提供缩小选项,因此无法捕获一个图上带有完整标签的整个图。

有人可以帮助我并提出解决方案吗?

for i in range(0,len(Graphs)):
    graph = Graphs[i]

    custom_labels={}
    node_colours=['y']
    for node in graph.nodes():
        custom_labels[node] = graph.node[node]
        node_colours.append('c')
    #nx.circular_layout(Graphs[i])

    nx.draw(Graphs[i], nx.circular_layout(Graphs[i]), node_size=1500, with_labels=True, labels = custom_labels, node_color=node_colours)
    #show with custom labels
    fig_name = "graph" + str(i) + ".png"
    #plt.savefig(fig_name)
    plt.show()

更新图片添加:

【问题讨论】:

标签: python-2.7 matplotlib networkx


【解决方案1】:

您可以使用font_size 参数来减小字体大小:

nx.draw(Graphs[i], nx.circular_layout(Graphs[i]), ... , font_size=6)

【讨论】:

  • 但是图形本身会确保它显示所有节点和完整标签的一种方式吗?
  • @aki 你能做一个可复制粘贴的小例子来说明问题所在吗?
  • 图片已添加,无法缩小以保存整个图形。那么还有其他解决方案吗?
  • @aki 你也可以添加 MCVE 复制粘贴代码吗?
  • 如果你能看这里会更好:github.com/marcincuber/modal_logic 然后运行 ​​logic_T 你就可以看到到底发生了什么
【解决方案2】:

你可以缩放图形

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edge('a'*50,'b'*50)
nx.draw(G,with_labels=True)
plt.savefig('before.png')
l,r = plt.xlim()
print(l,r)
plt.xlim(l-2,r+2)
plt.savefig('after.png')

之前

之后

【讨论】:

  • 我不得不说这是我没有想到的,而且效果很好,我可以接受它作为答案。我认为没有更好的解决方案。
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多