python词云

四行Python代码上手词云制作

1号词云:《葛底斯堡演说》黑色背景词云(4行代码上手)

import wordcloud
w = wordcloud.WordCloud()
w.generate('and that government of the people, by the people, for the people, shall not perish from the earth.')
w.to_file('output1.png')

运行完成之后,在代码所在的文件夹,就会出现output.png图片文件。可以看出,wordcloud自动将and that by the not from等废话词组过滤掉,并且把出现次数最多的people大号显示。

 

 

# 1号词云:葛底斯堡演说黑色背景词云
# B站专栏:同济子豪兄 2019-5-23
# 导入词云制作第三方库wordcloud
import wordcloud
# 创建词云对象,赋值给w,现在w就表示了一个词云对象
w = wordcloud.WordCloud()

# 调用词云对象的generate方法,将文本传入
w.generate('and that government of the people, by the people, for the people, shall not perish from the earth.')

# 将生成的词云保存为output1.png图片文件,保存出到当前文件夹中
w.to_file('output1.png')

wordcloud库为每一个词云生成一个WordCloud对象(注意,此处的W和C是大写)

也就是说,wordcloud.WordCloud()代表一个词云对象,我们将它赋值给w

现在,这个w就是词云对象啦!我们可以调用这个对象。

我们可以在WordCloud()括号里填入各种参数,控制词云的字体、字号、字的颜色、背景颜色等等。

wordcloud库会非常智能地按空格进行分词及词频统计,出现次数多的词就大。

 

posted @ 2021-12-16 13:07  青竹之下  阅读(53)  评论(0编辑  收藏  举报