我已经编写了一个示例代码来检查给定文件中每个文本的出现频率。请找到下面的代码
#Frequency of Given Search Key term from files
import os
import plistlib
import networkx as nx
import matplotlib.pyplot as plt
filename = str(raw_input("Enter the filename : "))
fp = open(filename,'r')
buffer1 = fp.read()
fp.close()
fp = open(filename,'r')
words = list(fp.read().split())
word = list(set(words))
fp.close()
G = nx.DiGraph()
for eachword1 in word:
cnt = buffer1.count(eachword1)
G.add_edge(eachword1,cnt,weight=0.9,color='blue',size=300)
print eachword1,"occurred",cnt," times"
nx.draw(G)
plt.show()
</i>在那里我可以得到图形,但在节点中没有文本。如何做到这一点?
发布于 2014-08-26 12:25:35
您已经绘制了图形,但尚未请求节点的名称。要绘制节点标签,请使用networkx包中的draw_networkx_labels。
下面是修改后的代码,在节点上有标签:如果你想让事情变得更漂亮,“position”可能应该使用"spring“之外的其他东西来设置:
import os
import plistlib
import networkx as nx
import matplotlib.pyplot as plt
filename = str(raw_input("Enter the filename : "))
fp = open(filename,'r')
buffer1 = fp.read()
fp.close()
fp = open(filename,'r')
words = list(fp.read().split())
word = list(set(words))
fp.close()
G = nx.DiGraph()
for eachword1 in word:
cnt = buffer1.count(eachword1)
G.add_edge(eachword1,cnt,weight=0.9,color='blue',size=300)
print eachword1,"occurred",cnt," times"
positions = nx.spring_layout(G)
nx.draw(G,positions)
nx.draw_networkx_labels(G,positions)
plt.show()明白了吗?
https://stackoverflow.com/questions/25497951
复制相似问题