puython输出高频词前50词云图
时间: 2024-05-01 18:18:43 浏览: 134
好的,以下是 Python 代码示例,可以帮助您输出高频词前50的词云图:
```python
# 导入所需的库
import jieba
from collections import Counter
from wordcloud import WordCloud
# 读取文本文件
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分词并统计词频
words = jieba.cut(text)
counter = Counter(words)
# 提取出现次数前50的关键词列表
keywords = []
for word, freq in counter.most_common(50):
keywords.append(word)
# 生成词云图
wordcloud = WordCloud(font_path='msyh.ttc', background_color='white', max_words=50).generate_from_frequencies(counter)
# 显示词云图
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
```
注意:此处使用的是 jieba 库进行中文分词,如果没有安装,请先执行以下命令进行安装:
```python
pip install jieba
```
阅读全文