Python模块:wordcloud库的使用

Python模块:wordcloud库的使用

wordcloud库基本介绍

wordcloud是优秀的词云展示第三方库
词云以词语为基本单位,更加直观和艺术的展示文本
wordcloud库安装
(cmd命令行) pip install wordcloud
Python模块:wordcloud库的使用_第1张图片

wordcloud库的使用说明

wordcloud库把词云当作一个WordCloud对象
wordcloud.WordCloud()代表一个文本对应的词云
可以根据文本中词语出现的频率等参数绘制词云
绘制词云的形状、尺寸和颜色都可以设定
w=wordcloud.WordCloud()
以WordCloud对象为基础
配置参数、加载文本、输出文件
方法:w.generate(txt)、w.to_file(filename)
Python模块:wordcloud库的使用_第2张图片
绘制步骤
步骤1:配置对象参数
:步骤2:加载词云文本
:步骤3:输出词云文件
import wordcloud
c = wordcloud.WordCloud()
c.generate("wordcloud by Python")
c.to_file("pywordcloud.png")

Python模块:wordcloud库的使用_第3张图片
Python模块:wordcloud库的使用_第4张图片

配置对象参数
w = wordcloud.WordCloud(<参数>)、min_font_size、max_font_size、font_step、font_path、max_words、stop_words、mask、background_color
Python模块:wordcloud库的使用_第5张图片
Python模块:wordcloud库的使用_第6张图片
Python模块:wordcloud库的使用_第7张图片
Python模块:wordcloud库的使用_第8张图片
import wordcloud
txt = "life is short,you need python"
w = wordcloud.WordCloud(background_color = "white")
w.generate(txt)
w.to_file("pywcloud.png")

Python模块:wordcloud库的使用_第9张图片
Python模块:wordcloud库的使用_第10张图片

你可能感兴趣的:(python)