pycharm词云图红楼梦
时间: 2023-12-07 15:40:07 浏览: 111
以下是在PyCharm中绘制红楼梦词云图的步骤:
1.安装必要的库
```shell
pip install jieba
pip install wordcloud
pip install imageio
```
2.导入必要的库
```python
import jieba
from wordcloud import WordCloud, ImageColorGenerator
import imageio
import matplotlib.pyplot as plt
```
3.读取红楼梦文本文件
```python
with open('红楼梦.txt', 'r', encoding='utf-8') as f:
text = f.read()
```
4.使用jieba库进行分词
```python
words = jieba.cut(text)
```
5.将分词结果转换为字符串
```python
words_str = ' '.join(words)
```
6.读取背景图片
```python
mask = imageio.imread('hongloumeng.jpg')
```
7.生成词云图
```python
wc = WordCloud(background_color='white', mask=mask, font_path='msyh.ttc', max_words=2000, max_font_size=200, random_state=42)
wc.generate(words_str)
```
8.根据图片生成颜色
```python
image_colors = ImageColorGenerator(mask)
```
9.绘制词云图
```python
plt.figure(figsize=(10, 10))
plt.imshow(wc.recolor(color_func=image_colors), interpolation='bilinear')
plt.axis('off')
plt.show()
```
阅读全文
相关推荐


















