我编写了一个示例代码来检查给定文件中每个文本的频率。 请在下面找到代码
#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>
我可以在哪里获取图表,但节点中没有文字。如何实现呢?
答案 0 :(得分:0)
您已绘制图表,但尚未请求节点的名称。要绘制节点标签,请在networkx包中使用draw_networkx_labels。
您的代码已修改为在节点上标记:&#34; position&#34;应该使用&#34; spring&#34;以外的东西来设置。如果你想让事情变得漂亮:
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()
清除?