对txt文件进行高频词汇可视化显示 生成词云图
时间: 2023-08-14 11:06:09 浏览: 43
您可以使用Python中的第三方库`wordcloud`来生成词云图。以下是一个简单的示例代码:
```python
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读取txt文件
with open('example.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 使用jieba分词
words = jieba.lcut(text)
# 去掉停用词
stopwords = {'的', '在', '了', '是', '和', '也', '就', '等'}
words = [word for word in words if word not in stopwords]
# 将分词结果转换成字符串
text = ' '.join(words)
# 生成词云图
wordcloud = WordCloud(background_color='white',
width=800, height=600,
font_path='simhei.ttf').generate(text)
# 显示词云图
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
```
在这个示例代码中,我们使用`jieba`对文本进行分词,并去掉了一些停用词。然后,使用`wordcloud`生成词云图,并使用`matplotlib`显示出来。
请注意,这个示例代码仅仅是一个基本的例子,你可以根据自己的需要进行修改和优化。