2016-02-25 85 views
0

我正在編寫一個程序,它爲特定的輸入字符串生成可滿足的模型(連接的圖形)。這裏的細節並不重要,但主要的問題是每個節點都有一個標籤,而且這個標籤可能很長。因此,會發生什麼情況是它不適合顯示所有節點的圖形,但是某些標籤部分顯示......而且,顯示的圖形不提供縮小的選項,因此無法捕捉完整的圖形與一個圖上的完整標籤。使用matplotlib缺少標籤的networkx圖形顯示

有人可以幫我,也許建議一個解決方案?

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() 

更新的圖片說: enter image description here

+0

http://stackoverflow.com/help/mcve – atomh33ls

回答

0

你可以 之前縮放圖

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') 

enter image description here

enter image description here

+0

我不得不說這是我沒有想到的東西它運作得很好,我可以接受它作爲答案。我認爲這個問題沒有更好的解決方案。 – aki

0

你可以減小字體大小,使用font_size參數:

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

不過是一種方式,數字本身將確保它顯示所有的節點和全標籤? – aki

+0

@aki可以製作一個小的可複製示例來說明發生了什麼問題嗎?添加了 – atomh33ls

+0

圖片,無法縮小以便保存整個圖形。那麼還有其他解決方案嗎? – aki