mac python 文本分析 词云
时间: 2023-12-19 13:32:26 浏览: 30
以下是使用Python在Mac上进行文本分析并创建词云的步骤:
1. 安装必要的库
```shell
pip install wordcloud numpy pillow matplotlib
```
2. 创建一个简单的心形词云
```python
from wordcloud import WordCloud
import matplotlib.pyplot as plt
text = "这是您要分析的文本内容"
# 生成词云
wordcloud = WordCloud(width=800, height=400, background_color='white', contour_color='steelblue').generate(text)
# 显示词云
plt.figure(figsize=(10,5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
```
3. 创建一个自定义形状的词云
```python
from wordcloud import WordCloud, ImageColorGenerator
from PIL import Image
import numpy as np
# 读取形状图片
mask = np.array(Image.open("heart_shape.png"))
# 生成词云
wordcloud = WordCloud(width=800, height=400, background_color='white', mask=mask, contour_color='steelblue').generate(text)
# 根据形状图片着色
image_colors = ImageColorGenerator(mask)
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis('off')
plt.show()
```