说明:基于DataFrame结构的词频表生成词云图
代码
from wordcloud import WordCloud
import matplotlib.pyplot as plt
frequencies = {}
count = 0
for line in open(".\ham.txt"):
count += 1
if count > 1:
arr = line.split(" ")
frequencies[arr[0].strip()] = int(arr[1].strip())
wc = WordCloud(
max_words=100,
width=400,
height=400,
)
word_cloud = wc.generate_from_frequencies(frequencies)
# 写词云图片
word_cloud.to_file("wordcloud_ham.jpg")
# 显示词云文件
plt.imshow(word_cloud)
plt.axis("off")
plt.show()
ham.txt文件已上传:ham.txt