python利用jieba和wordcloud生成词云图
时间: 2023-10-08 09:10:25 浏览: 130
1. 安装jieba和wordcloud库
```python
!pip install jieba
!pip install wordcloud
```
2. 导入库
```python
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
```
3. 导入文本数据
```python
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
```
4. 使用jieba进行分词
```python
text = ' '.join(jieba.cut(text))
```
5. 生成词云图
```python
wordcloud = WordCloud(font_path='msyh.ttc',
background_color='white',
width=800,
height=600).generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
```
完整代码:
```python
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
text = ' '.join(jieba.cut(text))
wordcloud = WordCloud(font_path='msyh.ttc',
background_color='white',
width=800,
height=600).generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
```
阅读全文