Python显示词云图
时间: 2023-11-19 17:50:35 浏览: 100
要显示词云图,可以使用Python的第三方库wordcloud。首先需要用pip安装该库,安装命令为:
pip install wordcloud
接下来就可以使用wordcloud创建词云图了。以下是一个简单的例子:
```python
import jieba
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. An interpreted language, Python has a design philosophy which emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly brackets or keywords), and a syntax which allows programmers to express concepts in fewer lines of code than might 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."
# 使用jieba分词
wordlist = jieba.cut(text, cut_all=True)
# 用空格连接分词结果,得到一个字符串
word_str = ' '.join(wordlist)
# 生成词云图
wc = WordCloud(background_color='white', max_words=2000, font_path='msyh.ttc')
wc.generate(word_str)
# 显示词云图
import matplotlib.pyplot as plt
plt.imshow(wc)
plt.axis('off')
plt.show()
```
以上代码将生成一个基本的英文词云图。中文词云图的生成也类似,只需替换掉分词器即可。
阅读全文