python绘制词云图
时间: 2023-09-02 16:12:05 浏览: 21
要绘制词云图,需要使用Python中的wordcloud库。以下是一个简单的例子:
首先,需要安装wordcloud库。可以使用pip命令进行安装:
```
pip install wordcloud
```
然后,导入需要的库:
```python
import wordcloud
from wordcloud import WordCloud
import matplotlib.pyplot as plt
```
接下来,读取文本文件并将其转换为字符串:
```python
with open("text.txt", "r") as file:
text = file.read().replace('\n', ' ')
```
然后,创建一个WordCloud对象并生成词云图:
```python
wordcloud = WordCloud(width = 800, height = 800,
background_color ='white',
min_font_size = 10).generate(text)
plt.figure(figsize = (8, 8), facecolor = None)
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad = 0)
plt.show()
```
这将生成一个简单的词云图。可以根据需要调整WordCloud对象的参数来改变词云图的外观。