wordcloud 库的使用
时间: 2023-09-27 11:07:11 浏览: 35
Wordcloud 是一个 Python 库,用于生成词云。它能够将一段文本中出现的单词按照频率生成词云图,让人们能够直观地了解文本的重点内容。
下面是一个简单的例子,演示了如何使用 wordcloud 库生成词云图:
```python
# 导入必要的库
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
# 生成词云图
text = "Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991. Python has a design philosophy which emphasizes code readability, and a syntax which allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable writing clear programs on both a small and large scale."
wordcloud = WordCloud().generate(text)
# 显示词云图
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
```
运行上述代码后,就可以看到生成的词云图了:
![wordcloud_example](https://user-images.githubusercontent.com/71109255/127745511-62e2cd0c-2e6e-4d8f-a53d-3d783212f8d5.png)
当然,wordcloud 还有很多参数可以调整,比如字体大小、颜色、背景色等等。你可以通过查看官方文档来了解更多信息。