统计完词频后,如何画词云图python代码
时间: 2024-03-05 07:54:23 浏览: 64
生成词云需要用到`wordcloud`库和`matplotlib`库,具体实现流程如下:
1. 安装`wordcloud`库和`matplotlib`库
```python
!pip install wordcloud
!pip install matplotlib
```
2. 读取文本并进行分词
```python
import jieba
# 读取文本文件
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 对文本进行分词
words = jieba.cut(text)
```
3. 统计词频并生成词云
```python
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 统计词频
word_counts = Counter(words)
# 生成词云
wc = WordCloud(width=800, height=600, background_color='white', font_path='simhei.ttf')
wc.generate_from_frequencies(word_counts)
# 显示词云
plt.imshow(wc)
plt.axis('off')
plt.show()
```
其中,`width`和`height`参数表示词云图的宽度和高度,`background_color`参数表示词云图的背景颜色,`font_path`参数表示词云图所使用的字体文件路径。
阅读全文
相关推荐


















