词云图python代码
时间: 2023-07-14 16:02:21 浏览: 45
以下是使用PyCharm编写的Python词云图代码:
```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()
# 对文本进行分词
words = jieba.cut(text)
# 将分词结果转换为字符串
text = ' '.join(words)
# 生成词云图
wc = WordCloud(background_color='white', width=800, height=600, font_path='msyh.ttc')
wc.generate(text)
# 显示词云图
plt.imshow(wc)
plt.axis('off')
plt.show()
```
其中,`text.txt`是要生成词云图的文本文件,`msyh.ttc`是字体文件,需要根据自己的系统和字体进行修改。
阅读全文
相关推荐








